Calculator
Calculator
l
Header Section
%{
#include "y.tab.h"
%}
This part is called the "header section" and is enclosed within `%{` and `%}`. It allows you to include C code
directly in the Lex file. Here, it includes the header file `y.tab.h`, which is generated by Yacc and contains
definitions for tokens.
Rules Section
%%
This is the "rules section" where you define patterns and corresponding actions. Each rule consists of a pattern on
the left and an action on the right.
`[0-9]+`: Matches one or more digits. When this pattern is matched, the associated action sets `yylval` to the
integer value of the matched text (`atoi(yytext)`) and returns the token type `NUM`.
`[-+*/\n]`: Matches one of the characters '-', '+', '*', '/', or '\n'. The associated action returns the ASCII value of the
matched character as the token.
`[ \t]`: Matches any space or tab character. The action is empty, indicating that whitespace characters are
ignored.
`.`: Matches any character not matched by the previous patterns. The action prints an error message to stderr,
indicating that an invalid character was encountered.
Additional Functions
int yywrap() {
return 1;
}
This function is called when the end of the input stream is reached. It returns 1, indicating that there is no more
input. In simple cases, this function can be left as is.
In summary, this Lex file defines patterns and associated actions to tokenize input expressions. It recognizes
numbers and operators and reports errors for invalid characters. The generated lexer (`lex.yy.c`) is used along
with the Yacc-generated parser (`y.tab.c`) to create a calculator program that can evaluate simple arithmetic
expressions.
Calculator.y
Header Section
%{
#include <stdio.h>
#include <stdlib.h>
%}
The header section includes C code that will be directly copied into the generated parser file (`y.tab.c`). It
includes necessary standard C library headers.
Yacc Declarations
%token NUM
%left '+' '-'
%left '*' '/'
These lines declare tokens and specify the precedence and associativity of operators. `NUM` is a token
representing numeric literals. The `%left` declarations specify the associativity of operators (`+` and `-` are left-
associative, also `*` and `/` are also left-associative).
Grammar Rules
%%
calc: /* empty */
| calc expr '\n' { printf("Result: %d\n", $2); }
;
These are the Yacc rules that define the grammar of the calculator language. The rules describe how to recognize
and construct abstract syntax trees for expressions.
`calc` is the starting rule, representing a sequence of expressions. When an expression is encountered followed
by a newline character, it prints the result of the expression.
`expr` defines the grammar for arithmetic expressions. It handles numeric literals, addition, subtraction,
multiplication, and division. Division by zero is checked, and an error message is printed if encountered.
main Function
int main() {
yyparse();
return 0;
}
The main function is where the parser is invoked. It calls `yyparse()`, which is a function generated by Yacc to
initiate the parsing process.