Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bison: Parser Generator

Similar presentations


Presentation on theme: "Bison: Parser Generator"— Presentation transcript:

1 Bison: Parser Generator
CSE470: Spring 2000 Updated by Prasad

2 Motivation Bison: A general purpose Parser Generator that converts a CFG (Context Free Grammar) description into a C program to parse that grammar. It is also called as YACC (Yet Another Compiler Compiler). Integrates smoothly with Flex for Text Processing. Responsible for deducing the relationship between the tokens passed on to it by the Lexical Analyzer.

3 Example for a Grammar Statement  NAME ‘=‘ Expression | Expression ;
Expression NUMBER | Expression ‘+’ NUMBER | Expression ‘-’ NUMBER This grammar can generate expressions like: fred = fred = –

4 Bison Parser Reads sequence of Tokens as Input
Groups tokens using Grammar rules recursively. (Actually builds a Parse Tree for the input token sequence). Valid Input: Entire token sequence reduces to single grouping whose symbol is Grammar’s start symbol. Invalid Input: Parser reports syntax error if token sequence cannot be reduced to START symbol of grammar.

5 Bison Parser yyparse()
Input Text File Lexical Analyzer Call for token token Bison Grammar Files Rules Bison Parser yyparse()

6 Bison Source Consists of four sections: %{ C Declarations %}
Bison Declarations %% Grammar Rules Section %% Additional C Code

7 Example Bison Rules %token NAME NUMBER %%
Statement: NAME ‘=‘ expression | expression { printf(“= %d\n”, $1); } ; expression: expression ‘+’ NUMBER {$$ = $1 + $3; } | expression ‘-’ NUMBER {$$ = $1 - $3; } | NUMBER {$$ = $1;}

8 Sample Flex Program for this parser
%{ extern int yylval; %} %% [a-zA-Z] return NAME; [0-9] {yylval = atoi(yytext); return NUMBER; } [ \t] ; /* ignore spaces */ \n return 0; return yytext[0]


Download ppt "Bison: Parser Generator"

Similar presentations


Ads by Google