Skip to content

Semantic section

The semantic section lets you implement language semantics by injecting target-language code into classes generated from the syntactic section.

Section header

A bare % line separates the previous section from the semantic section. The first non-blank line of the semantic section body names the target language.

%
javascript

Code blocks

To inject code into a class, name the class and follow it with a %%% block:

ClassName
%%%
# your code here
%%%

The block's content is injected into the class body. Multiple blocks for the same class are injected in the order they appear.

Field access

Fields generated from production rules become instance variables in the generated class. For example:

<AddExp> ::= <Exp:left> PLUS <Exp:right>

generates a class with fields left and right. How you access them depends on your target language — see your language's page below.

Entry point: _run

The start symbol's class inherits from _Start, which defines a _run method that is called when you run a program with plcc-rep. _run returns its output as a string; plcc-rep prints what it returns. The default implementation returns a string representation of the parse tree root. Override _run in your start class to implement your language's semantics.

Returning something other than a string is a specification error, so convert explicitly when your semantics produce another type. Do not print or write to stdout from inside _run — that bypasses plcc-rep's result protocol. plcc-rep echoes the stray text as-is, so it surfaces in every verbose format: under plcc-rep --verbose-format=json it lands among the JSON records as a line that is not JSON, breaking any consumer that parses the stream.

Signatures differ by target language — see your language's page below.

Signaling errors from semantics

When your semantics code needs to report a deliberate error — a type mismatch, a precondition violation, or any condition your language treats as an error — raise a LanguageError. plcc-rep prints the message and gives a fresh prompt; the session continues.

Raising any other exception is treated as a bug in your specification. plcc-rep prints a specification error and exits.

Each language provides LanguageError as part of its generated runtime. See your language's page for the exact syntax.

Hooks

Hooks inject code at specific locations within a generated class file. Append the hook name to the class name with a colon:

ClassName:hook
%%%
# code injected at the hook location
%%%
Hook Where code is injected
top Top of the generated file
import File-level import section
class Class declaration line (extend/implement)
init Constructor / initializer

Supported hooks and their exact placement vary by language. See your language's page for the full list and any language-specific restrictions.

Adding standalone classes

You can create a file that is not derived from a grammar nonterminal. Name a class that does not appear in the grammar and follow it with a %%% block; plcc-ng writes the block's content verbatim to a file named after the class.

Choose your language

Each language page covers: prerequisites, enabling in a spec, a quick reference example, how BNF constructs map to language constructs, supported hooks, the _run contract, generated output, commands, restrictions, and tips.