Created
December 19, 2019 07:06
-
-
Save Ch3shireDev/56cc135c92c57a31cc7087963c8b9313 to your computer and use it in GitHub Desktop.
TatSu grammar
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
| from pprint import pprint | |
| from tatsu import parse | |
| grammar = ''' | |
| @@grammar::Grammar | |
| start = 'if' ['happens that'] expression ['then'] ['return'] ['error']$ ; | |
| expression = condition [and condition]; | |
| condition = | |
| | 'A is B' | |
| | 'B is C' | |
| | 'A = B' | |
| | 'B != C' | |
| | 'element A is B' | |
| | 'object C is not equal to D' | |
| ; | |
| or = 'or'; | |
| and = 'and'; | |
| ''' | |
| code = ''' | |
| if | |
| A is B | |
| and | |
| B is C | |
| then error | |
| ''' | |
| code2=''' | |
| if happens that | |
| A = B | |
| and | |
| B != C | |
| then return error | |
| ''' | |
| code3=''' | |
| if | |
| element A is B | |
| and | |
| object C is not equal to D | |
| then error | |
| ''' | |
| ast = parse(grammar, code) | |
| pprint(ast, indent=2, width=20) | |
| ast = parse(grammar, code2) | |
| pprint(ast, indent=2, width=20) | |
| ast = parse(grammar, code3) | |
| pprint(ast, indent=2, width=20) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment