Skip to content

Instantly share code, notes, and snippets.

@filhodanuvem
Last active December 15, 2015 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filhodanuvem/5189574 to your computer and use it in GitHub Desktop.
Save filhodanuvem/5189574 to your computer and use it in GitHub Desktop.
How a simple parser to python can generate a AST ?
def foo():
name = "cloud"
age = 22
if age > 22 :
print name
age += 1
@jeanpimentel
Copy link

T_FUNCTION T_STR(foo) T_PAREN_OPEN T_PAREN_CLOSE
  T_VAR(name) T_ATRIB T_STR(cloud)
  T_VAR(age) T_ATRIB T_INT(22)
  T_IF T_VAR(age) T_PLUS T_INT(22)
T_IDENT_START T_FUNC(print) T_VAR(name)
T_IDENT_END T_VAR(age) T_PLUS_ATRIB T_INT(1)


@jeanpimentel
Copy link

no lexico, se leu \n, vai pro estado qn

leia o proximo, se \s ou \t, conte e armazene até sair de qn

ao sair de qn

se atual > anterior => T_IDENT_START
senao se atual = anterior => null
senao => T_IDENT_END

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment