Lex and Yacc
Lex and Yacc
Lex and Yacc
https://www-01.ibm.com/support/knowledgecente...
Content
Specifies the lex command specification file that defines the lexical analysis rules. For more
information, see Lexical Analyzer Source Code.
Specifies the yacc command grammar file that defines the parsing rules, and calls the yylex
subroutine created by the lex command to provide input. For more information, see Parser
Source Code.
calc.yacc
The following descriptions assume that the calc.lex and calc.yacc example programs are located in your
current directory.
1 of 5
https://www-01.ibm.com/support/knowledgecente...
2 of 5
'|'
'&'
'+' '-'
'*' '/' '%'
UMINUS /*supplies precedence for unary minus */
/* beginning of rules section */
/*empty */
https://www-01.ibm.com/support/knowledgecente...
|
list stat '\n'
|
list error '\n'
{
yyerrok;
}
;
stat:
expr
{
printf("%d\n",$1);
}
|
LETTER '=' expr
{
regs[$1] = $3;
}
;
expr:
3 of 5
= $1 * $3;
'/' expr
= $1 / $3;
'%' expr
= $1 % $3;
'+' expr
= $1 + $3;
'-' expr
= $1 - $3;
'&' expr
= $1 & $3;
Sunday 27 December 2015 05:30 PM
https://www-01.ibm.com/support/knowledgecente...
}
|
expr '|' expr
{
$$ = $1 | $3;
}
|
'-' expr %prec UMINUS
{
$$ = -$2;
}
|
LETTER
{
$$ = regs[$1];
}
|
number
;
number:
DIGIT
{
$$ =
base
}
number
{
$$ =
}
;
$1;
= ($1==0) ? 8 : 10;
|
DIGIT
base * $1 + $2;
%%
main()
{
return(yyparse());
}
yyerror(s)
char *s;
{
fprintf(stderr, "%s\n",s);
}
yywrap()
{
return(1);
}
The file contains the following sections:
Declarations Section. This section contains entries that:
Include standard I/O header file
4 of 5
https://www-01.ibm.com/support/knowledgecente...
5 of 5