Professional Documents
Culture Documents
CS419 Lecture 15
CS419 Lecture 15
Syntax Analysis
[Chapter 4 - Parts 7&8]
Lecture 15
• Lexical Analyzer:
– Lex/Flex is a computer program that converts
the text into tokens. It is used to identify
specific text strings in a structured way from
source text.
• Parser Generators:
– Yacc/ Bison (Yet Another Compiler Compiler)
is a computer program that generates LALR(1)
parsers.
Creating a Lexical Analyzer &
3
lex.yy.c C a.out
compiler
input sequence
stream a.out of tokens
Generating LALR(1) Parser with
4
Yacc/Bison
yacc yacc.y
contains yacc Yacc / Bison y.tab.c
specification compiler
input output
stream a.out stream
5
%{ #include <ctype.h> %}
%token DIGIT
%%
line : expr ‘\n’ { printf(“%d\n”, $1); }
;
expr : expr ‘+’ term { $$ = $1 + $2; }
| term { $$ = $2; }
;
term : term ‘*’ factor { $$ = $2 * $3; }
| factor { $$ = $3; }
;
factor : ‘(’ expr ‘)’ { $$ = $1; }
| DIGIT { $$ = $1; }
; Attribute of factor(child)
%% Attribute of
int yylex() term(parent) Attribute of token
{ int c = getchar(); (stored in yylval)
if (isdigit(c))
{ yylval = c-’0’; yylval passes the semantic value associated with a
return DIGIT; token from the lexer to the parser.
}
return c;
if c is a digit, then it is returned as a DIGIT token,
} otherwise c is returned as the token name
9
%{
#include <ctype.h>
#include <stdio.h> Double type for attributes
#define YYSTYPE double and yylval
%} a union with a member for each type
%token NUMBER of token defined within the yacc
%left ‘+’ ‘-’ source file
%left ‘*’ ‘/’
%right UMINUS
%%
lines : lines expr ‘\n’ { printf(“%g\n”, $2); }
| lines ‘\n’
| /* empty */
;
expr : expr ‘+’ expr { $$ = $1 + $3; }
| expr ‘-’ expr { $$ = $1 - $3; }
| expr ‘*’ expr { $$ = $1 * $3; }
| expr ‘/’ expr { $$ = $1 / $3; }
| ‘(’ expr ‘)’ { $$ = $2; }
| ‘-’ expr %prec UMINUS { $$ = -$2; }
| NUMBER to give a grammar rule a precedence uses
; the %prec TOKEN
%%
11
int yylex()
{ int c;
while ((c = getchar()) == ‘ ‘)
;
if ((c == ‘.’) || isdigit(c))
Simple lexical analyzer
{ ungetc(c, stdin); //push back last char read for floating point doubles
scanf(“%lf”, &yylval); and arithmetic operators
return NUMBER;
}
return c; returns a value of 0 if the
} input it parses is valid
int main()
{ if (yyparse() != 0)
fprintf(stderr, “Abnormal exit\n”);
Run the parser
return 0;
}
int yyerror(char *s) Invoked by parser
{ fprintf(stderr, “Error: %s\n”, s);
to report parse errors
}
Combining Lex/Flex with 12
Yacc/Bison
lex.yy.c C a.out
y.tab.c compiler
input output
stream a.out stream
13
Error production:
Reset parser to normal mode
set error mode and
skip input until newline