Parser - o2bblockbusting/cs421_project GitHub Wiki

What does the parser do?

The parser converts a list of sequential tokens into a tree of expressions that can be easily understood and executed by an interpreter. There are different types of expressions like conditionals, loops, literal, variables, etc. that represent different parts of the program structure. The parsing stage is also where any syntax errors are caught and displayed to the user. The parser is written in Java like the rest of the program with no external dependencies.

Grammar

program β†’ block

block β†’ statement+

statement β†’ assignment | while | if | function_call | print | array_func_call | return | break | continue | quit

assignment β†’ ("A" IDENTIFIER TYPE ("[]")? "V" comparison1) | function_def

function_def β†’ "A" IDENTIFIER "F" TYPE? "V(" (TYPE IDENTIFIER)? ("," TYPE IDENTIFIER)* ")Y" block "Z"

while β†’ "W" comparison1 "Y" block "Z"

if β†’ "I" comparison1 "Y" block "Z" ("E" (if | "Y" block "Z"))?

function_call β†’ IDENTIFIER "(" (comparison1)? ("," comparison1)* ")"

array_func_call→ IDENTIFIER "~" IDENTIFIER "(" (comparison1)? ("," comparison1)* ")"

print β†’ "P(" (comparison1)? ("," comparison1)* ")"

return β†’ "R" comparison1

break β†’ "J"

continue β†’ "K"

quit β†’ "Q"

comparison1 β†’ comparison2 ( "|" comparison2 )*

comparison2 β†’ comparison3 ( "&" comparison3 )*

comparison3 β†’ term ( ( "<" | ">" | "<=" | ">=" | "=" | "!=" ) term )*

term β†’ factor ( ( "-" | "+" ) factor )* ;

factor β†’ unary ( ( "/" | "" | "%") unary ) ;

unary β†’ ("-" | "+" | "!") unary | primary ;

primary β†’ NUMBER | IDENTIFIER | function_call | array_func_call | "( cast_type )" | "(" expression ")" ;

TYPE is Long L, Int N, Double D, Float G, String S, Char C, Bool B

⚠️ **GitHub.com Fallback** ⚠️