This project implements the lexical analysis and CFG-based syntactic analysis (recursive-descent parser) phases for the course assignment. The repository is designed to consume the token stream produced by the lexer, parse the program according to a context-free grammar, and demonstrate leftmost/rightmost derivations and parse trees as required by Question 2.
Question 2 asks you to:
- Design a complete Context-Free Grammar (CFG) that generates the core language (declarations, assignments, arithmetic and boolean expressions, if-else, while, blocks, print).
- Ensure the grammar can generate the provided evaluation program without modification.
- Demonstrate syntactic validation by showing a leftmost derivation, a rightmost derivation, and the parse tree for at least one non-trivial statement.
- The parser must consume the token stream produced by the lexical analyzer.
- Grammar: The CFG is printed by
parser.pyvia theprint_cfg()function and is the grammar implemented by the parser. It includes productions forprogram,stmt_list,stmt,decl_stmt,assign_stmt,if_stmt,while_stmt,print_stmt,block,bool_expr,bool_term,bool_factor,comparison,expr,term, andfactor. - Lexer → Parser:
main()inparser.pycreates aLexer(source), callstokenize()and then passes the returned token list toParser(tokens)— so the parser directly consumes the lexer’s token stream. - Derivations & Parse Tree: After successful parsing the program,
parser.pycollects top-level statements and prompts the user to select a statement (orall). For each selected statement it prints:- leftmost derivation (via
derive_leftmost), - rightmost derivation (via
derive_rightmost), and - the parse tree (via
print_tree).
- leftmost derivation (via
These outputs directly demonstrate syntactic validation exactly as required by Question 2.
-
lexer.py- Implements token definitions using regex and a
Lexerclass. tokenize()returns a list ofToken(type, lexeme, line, column)and appends anEOFtoken.print_token_table()shows the token stream (used for Question 1).
- Implements token definitions using regex and a
-
parser.pyNode: lightweight parse-tree node (name, children, optionaltoken).Parser(recursive-descent): operates over a token list with methods:program(),stmt_list(),stmt()— top-level controldeclaration(),assignment(),if_stmt(),while_stmt(),print_stmt(),block()— statement-level rulesbool_expr(),bool_term(),bool_factor(),comparison()— boolean and relational handlingexpr(),term(),factor()— arithmetic expressions with precedence
- Error handling:
expect()raisesParseErrorwith line/col for clear diagnostics. - Derivation helpers:
derive_leftmost,derive_rightmost,render_formbuild and show derivation steps. collect_statements()finds derivable statements for demonstration;show_derivations()prints derivations + parse tree.
- Print tokens (Question 1):
python lexer.py evaluation_program.src- Parse and interactively show derivations (Question 2):
python parser.py evaluation_program.srcWhen prompted, enter a statement number, a source line number, comma list, or all.
To script showing the while statement (example):
printf '8\n' | python parser.py evaluation_program.srcTo show derivations for all statements:
printf 'all\n' | python parser.py evaluation_program.src- Show the token stream from
lexer.pyto demonstrate Question 1. - Use
parser.pyand select a non-trivial statement (e.g., thewhileloop or theif (!(avg < 5.0))statement) and present:- leftmost derivation
- rightmost derivation
- the parse tree
These three outputs satisfy the syntactic validation requirements of Question 2.
- Add semantic analysis: symbol table with scopes, undeclared/multiple declaration checks, and type checking.
- Generate intermediate code (three-address code) and apply simple optimizations.
- Add a small README section for how to present outputs in the viva.
If you want, I can now:
- add a short “presenter’s notes” section to this README with exact terminal commands and sample screenshots of outputs, or
- implement the symbol table and basic semantic checks next.
If you want me to proceed, tell me which next step to take.
hello world