Skip to content

Instantly share code, notes, and snippets.

@Ch3shireDev
Created December 19, 2019 07:06
Show Gist options
  • Select an option

  • Save Ch3shireDev/56cc135c92c57a31cc7087963c8b9313 to your computer and use it in GitHub Desktop.

Select an option

Save Ch3shireDev/56cc135c92c57a31cc7087963c8b9313 to your computer and use it in GitHub Desktop.
TatSu grammar
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