Parser Generator
The parser generator converts context-free grammars into LALR(1) parsing tables and generates parser code.
How It Works
- Parsing: The
.yfile is parsed to extract tokens, precedence, and grammar rules. - Grammar Analysis: First and Follow sets are computed.
- LR(0) Items: Canonical LR(0) item sets are constructed.
- LALR(1) Tables: Lookahead information is computed and tables are built.
- Conflict Resolution: Shift/reduce and reduce/reduce conflicts are resolved using precedence.
- Code Generation: The parsing table and driver are emitted in the target language.
Input Format
Grammar specifications use the Bison .y file format:
%{
/* Prologue code */
%}
/* Token and precedence declarations */
%token NUMBER PLUS MINUS
%left PLUS MINUS
%%
/* Grammar rules */
expr: expr PLUS expr
| expr MINUS expr
| NUMBER
;
%%
/* Epilogue code */
Output
The generated parser provides:
- A
Parserclass (or struct in C) - A
parse()function that returns the parse result - Action and goto tables
- Semantic action execution
GLR Mode
For ambiguous grammars, add %glr-parser to enable GLR parsing:
%glr-parser
%%
expr: expr MINUS expr
| NUMBER
;
%%
Chapters
- Grammar File Format - Complete
.yfile syntax - Tokens and Precedence - Declaring tokens and resolving conflicts
- Semantic Actions - Attaching code to rules
- Error Handling - Parser error recovery