Example Program For The Lex and Yacc Programs
Example Program For The Lex and Yacc Programs
Example Program For The Lex and Yacc Programs
This section describes example programs for the lex and yacc commands.
Together, these example programs create a simple, desk-calculator program that
performs addition, subtraction, multiplication, and division operations. This
calculator program also allows you to assign values to variables (each designated
by a single, lowercase letter) and then use the variables in calculations. The files
that contain the example lex and yacc programs are:
File
Content
calc.lex Specifies the lex command specification file that defines the lexical analysis rules.
calc.yacc Specifies the yacc command grammar file that defines the parsing rules, and calls
the yylex subroutine created by the lex command to provide input.
The following descriptions assume that the calc.lex and calc.yacc example
programs are found in your current directory.
Compiling the Example Program
Perform the following steps, in order, to create the desk calculator example
program:
1. Process the yacc grammar file using the -d optional flag (which tells
the yacc command to create a file that defines the tokens used in addition to
the C language source code):
yacc -d calc.yacc
2. Use the li command to verify that the following files were created:
y.tab.c The C language source file that the yacc command created for the parser.
y.tab.h A header file containing define statements for the tokens used by the parser.
4. Use the li command to verify that the following file was created:
lex.yy.c The C language source file that the lex command created for the lexical analyzer.
6. Use the li command to verify that the following files were created:
y.tab.o
lex.yy.o
a.out
7. To then run the program directly from the a.out file, enter:
8. $ a.out
9. Or, to move the program to a file with a more descriptive name, as in the
following example, and run it, enter:
10. $ mv a.out calculate
11. $ calculate
12.In either case, after you start the program, the cursor moves to the line below
the $ (command prompt). Then enter numbers and operators in calculator
fashion. When you press the Enter key, the program displays the result of
the operation. After you assign a value to a variable:
13. m=4 <enter>
14. _
15.the cursor moves to the next line. When you use the variable in subsequent
calculations, it will have the assigned value:
16. m+5 <enter>
17. 9
18. _
stat:
expr:
yyerrok;
}
;
expr
{
printf("%d\n",$1);
}
|
LETTER '=' expr
{
regs[$1] = $3;
}
;
'(' expr ')'
{
$$ = $2;
}
|
expr '*' expr
{
$$ = $1 * $3;
}
|
expr '/' expr
{
$$ = $1 / $3;
}
|
expr '%' expr
{
$$ = $1 % $3;
}
|
expr '+' expr
{
$$ = $1 + $3;
}
|
expr '-' expr
{
$$ = $1 - $3;
}
|
expr '&' expr
{
$$ = $1 & $3;
}
|
expr '|' expr
{
$$ = $1 | $3;
}
|
'-' expr %prec UMINUS
{
$$ = -$2;
}
|
LETTER
{
number:
$$ =
}
|
number
;
DIGIT
{
$$ =
base
}
number
{
$$ =
}
;
regs[$1];
$1;
= ($1==0) ? 8 : 10;
|
DIGIT
base * $1 + $2;
%%
main()
{
return(yyparse());
}
yyerror(s)
char *s;
{
fprintf(stderr, "%s\n",s);
}
yywrap()
{
return(1);
}
Declarations Section
The rules section defines the rules that parse the input stream.
Programs Section
main
The required main program that calls the yyparse subroutine to start the program.
yyerror(s) This error-handling subroutine only prints a syntax error message.
yywrap The wrap-up subroutine that returns a value of 1 when the end of input occurs.