LRSTAR - Parser Generator for C++ | A.M.D.G. |
About
Feedback
Installation and Setup LRSTAR DFA Papers Release Notes Contact, Support |
LRSTAR Options Type "lrstar" and you get this: LRSTAR 24.0.000 64b Copyright Paul B Mann. | | LR(*) PARSER GENERATOR | | lrstar <grammar> [/<option>...] | | OPTION DEFAULT DESCRIPTION | | crr 0 Conflict report for Reduce-Reduce | csr 0 Conflict report for Shift-Reduce | fsa 0 Force shift actions in conflicts | | g 1 Grammar listing | gh 0 Grammar HTML format output | | st 0 States listing | ci 0 Case-insensitive input | | pt 1 Parser-table creation | o 0 Optimize parser-tables | m 0 Minimize parser-tables | | k 1 k-lookaheads for LR(*) parsing | | ast 1 AST activated in parser | exp 1 Expecting-list activated | ta 1 Terminal-actions activated | na 1 Node-actions activated | | d 0 Debug parser activated. | dt 0 Debug trace activated. | | v 2 Verbose listing (0,1,2) | w 0 Warnings listed on screen | | wa 0 Warning, arguments -> nonterminals | wc 0 Warning, constants not used | wk 0 Warning, keywords not declared | wm 0 Warning, mixed case keywords | wu 0 Warning, upper case keywords |_ LRSTAR Grammars LRSTAR reads a grammar. A grammar is a "list of rules" specifying (1) the pattern of an input file, or (2) the syntax of a language. LRSTAR reads pure grammar notation, mostly rules, action operators and function names. By using AST operators, LRSTAR parsers can construct an abstract-syntax tree (AST). Simple, concise and powerful. Lexical Tokens
<identifier>,
<integer>,
<string>,
<eof>. <error> => error(); <identifier> => lookup(); // Call symbol-table function. <integer> => lookup(); // Call symbol-table function. { '+' '-' } << // Left associative, lowest priority. { '*' '/' } << // Left associative, highest priority. Start -> Program+ <eof> *> goal_ Program -> 'program' <identifier> '{' Stmt* '}' *> program_(2) Stmt -> Target '=' Exp ';' *> store_~ Target -> <identifier> *> target_(1) Exp -> <integer> *> integer_(1) -> <identifier> *> identifier_(1) -> Exp '+' Exp *> add_ -> Exp '-' Exp *> sub_ -> Exp '*' Exp *> mul_ -> Exp '/' Exp *> div_ -> '(' Exp ')' Lexical Tokens As Defined Constants
IDENTIFIER,
INTEGER,
STRING,
EOF. However, you may have a YACC grammar which uses defined constants in the rules. In this case, you will need to put the defined constants at the top of your grammar (easier than changing the rules). These defined constants will also be available to use in your source code. Here is an example: ERROR <error> => error(); IDENTIFIER <identifier> => lookup(); // Call symbol table function. INTEGER <integer> => lookup(); // Call symbol table function. EOF <eof>; PLUS '+'; MINUS '-'; MUL '*'; DIV '/'; LBRACE '{'; RBRACE '}'; EQUALS '='; SEMICOLON ';'; LPAREN '('; RPAREN ')'; PROGRAM 'program'; Parser-Defined Terminals
{typename},
{headsymbol},
{function_name},
{array_name}. <identifier> => lookup(); // Call symbol-table function. Goal : Declaration* <eof> ; Declaration : TypeSpec+ Identifier ';' | typedef TypeSpec+ Typename ';' ; Identifier : <identifier> ; Typename : <identifier>^{typename} ; TypeSpec : char | int | short | {typename} ; Two Special Symbols
<eof> End-of-file symbol.
<eof> Goal -> CompilationUnit <eof>
<error> EBNF Notation
Basic operators: Extended operators: Here are some useful examples: Goal -> ExternalDef* <eof> // Zero or more ExternalDef PrimaryExp -> <string>+ // One or more <string> -> <identifier> Default -> (default ':' Stmt*)? // Optional group PostfixExp -> PrimaryExp -> {function} '(' [Args] ')' // Optional Args Args -> <identifier>/','+ // One or more separated by ','s Three Action Operators
=> Call a Token Action. Here is a simple grammar, with action operators: <error> => error(); <identifier> => lookup(); // Call symbol table function. <integer> => lookup(); // Call symbol table function. { '+' '-' } << // Left associative, lowest priority. { '*' '/' } << // Left associative, highest priority. Goal -> Program+ <eof> *> goal_ Program -> 'program' <identifier> '{' Stmt* '}' *> program_(2) Stmt -> Target '=' Exp ';' *> store_~ Target -> <identifier> *> target_(1) Exp -> <integer> *> integer_(1) -> <identifier> *> identifier_(1) -> Exp '+' Exp *> add_ -> Exp '-' Exp *> sub_ -> Exp '*' Exp *> mul_ -> Exp '/' Exp *> div_ -> '(' Exp ')' Reverse Operator ~ Reverse the child nodes. In the AST, some subtrees work much better when the order is "reversed" from the original input order. In the following case, we want to evaluate the Exp before we store the result in the Target, so we use the ~ after the Node Action name. See the Calc project. Stmt -> Target '=' Exp ';' *> store_~ |
(c) Copyright Paul B Mann 2023. All rights reserved. |