This repository contains C code implementing a simple syntax analyzer along with a lexer to generate a symbol table for arithmetic expressions.
This file comprises functions to parse arithmetic expressions based on a simple grammar, utilizing a recursive descent parser approach.
E(),Eprime(),T(),Tprime(),F(): Functions implementing parsing rules for non-terminals in the grammar.generateCode(): Generates intermediate code for expressions.main(): Accepts an arithmetic expression as input, parses it, and verifies its validity.
The lexer (lex() function) tokenizes the input string into tokens based on predefined token types:
TokenStructure: Containstype(enumerated token type) andvalue(if applicable).lex()Function: Tokenizes the input expression.printTokens()andfreeTokens()Functions: Aid in printing tokens and freeing allocated memory, respectively.
This file represents a basic symbol table functionality:
SymbolEntryStructure: Defines a symbol entry with attributes (e.g., name, type, value, scope).symbolTableArray: Stores symbol entries.addSymbol()andfindSymbol()Functions: Add and search for symbols in the table, respectively.
- Compile the code using a C compiler (
gccrecommended). - Run the executable generated after compilation, providing an arithmetic expression as input.
- The code will verify the validity of the expression and generate a symbol table for predefined symbols.
Consider the input expression (i + i * i). Upon execution, the code will parse the expression and output whether it's valid or invalid, along with a symbol table if valid.
- This code serves as a basic demonstration and might need enhancements for more complex grammars or symbol table requirements.
- Modify and extend the code according to specific grammar or symbol table needs.