Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Created a compiler for mathematial expression with +, -, *, /, (,) Precedence is taken care

Lexical Analysis (Lexer - calc.l) - pattern and actions Syntax Analysis (Parser - calc.y) - Grammer rules without code generation

.l -> has all the patterns and actions - when a pattern is matched the specific action is taken to return the token to the parser

.y -> Grammer rules and precedence

lex calc.l -> generates lex.yy.c which contains the yylex() function,which scans the input character by character, matches patterns, and returns tokens to Yacc.

byacc -d calc.y -> generates y.tab.c and y.tab.h

The yywrap() function is called when the input stream reaches EOF. yyparse() processes tokens returned by yylex() and applies grammar rules defined in calc.y. Yacc creates a yyparse() function in y.tab.c based on the grammar in calc.y. yyparse() calls yylex() repeatedly to retrieve tokens and applies grammar rules to them.

calc.y defines tokens using %token (e.g., NUMBER). calc.l includes y.tab.h to recognize and use these tokens.

lex.yy.c: Implements yylex() - Matches patterns from calc.l. - Returns tokens and assigns values to yylval. y.tab.h: Defines tokens and their values y.tab.c: Implements yyparse() - Processes tokens from yylex() - Applies grammar rules.

gcc lex.yy.c y.tab.c -o calc ./calc

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages