Start conditions are declared in the definitions (first) section of the input using unindented lines beginning with either %s
or %x
followed by a list of names. The former declares inclusive start conditions, the latter exclusive start conditions. A start condition is activated using the BEGIN action. Until the next BEGIN action is executed, rules with the given start condition will be active and rules with other start conditions will be inactive. If the start condition is inclusive, then rules with no start conditions at all will also be active. If it is exclusive, then only rules qualified with the start condition will be active. A set of rules contingent on the same exclusive start condition describe a scanner which is independent of any of the other rules in the flex input. Because of this, exclusive start conditions make it easy to specify "mini-scanners" which scan portions of the input that are syntactically different from the res
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Calculator grammar for a parser in Python. | |
* | |
* syntax-cli -g calculator.grammar.js -m LALR1 -o calcparser.py | |
* | |
* >>> import calcparser | |
* >>> calcparser.parse('2 + 2 * 2') | |
* >>> 6 | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import calcparser | |
print(calcparser.parse('2 + 2 * 2')) # int(6) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Calculator grammar for a parser in JavaScript. | |
* | |
* syntax-cli -g calculator.ast.js -m LALR1 -o CalcParser.js | |
* | |
* const CalcParser = require('./CalcParser.js'); | |
* console.log(CalcParser.parse('2 + 2 * 2')); // AST | |
*/ | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CalcParser = require('./CalcParser.js'); | |
console.log(CalcParser.parse('2 + 2 * 2')); // AST | |
/* | |
Result: | |
{ | |
type: "Binary", | |
left: { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Calculator grammar for a parser in Python. | |
* | |
* syntax-cli -g calculator.ast.php.js -m LALR1 -o CalcParser.php | |
* | |
* <?php | |
* | |
* require('CalcParser.php'); | |
* var_dump(CalcParser::parse('2 + 2 * 2')); | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require('CalcParser.php'); | |
var_dump(CalcParser::parse('2 + 2 * 2')); | |
/* | |
Result: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Explicit handling of the operator associativity. See also implicit handling | |
* https://gist.github.com/DmitrySoshnikov/ab6ed4d41505c579cee9af759e77ead9 | |
* | |
* Calculator grammar for a parser in Python. | |
* | |
* syntax-cli -g calc-no-assoc.g.js -m LALR1 -o calcparser.py | |
* | |
* >>> import calcparser | |
* >>> calcparser.parse('2 + 2 * 2') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.dmitrys:syntax$ syntax-cli --grammar ~/calculator.grammar.js --mode LALR1 --table | |
Parsing mode: LALR(1). | |
Grammar: | |
0. $accept -> E | |
---------------- | |
1. E -> E + E | |
2. | E * E |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% | |
boolean | |
: term bool' | |
; | |
bool' | |
: 'OR' term bool' | |
| /* epsilon */ | |
; |