Docsity

Parser Generators - Compiler Construction - Lecture Notes, Study notes of Compiler Construction

Toggle sidebar
bg1
Sohail Aslam Compiler Construction Notes Set:2-1
1
L
Le
ec
ct
tu
ur
re
e
3
30
0
Parser Generators
Parser generators exist for LL(1) and LALR(1) grammars. For example,
• LALR(1) - YACC, Bison, CUP
• LL(1) – ANTLR
• Recursive Descent - JavaCC
YACC Parser Generator
YACC – Yet Another Compiler Compiler, appeared in 1975 as a Unix application. The
other companion application Lex appeared at the same time. These two greatly aided the
construction of compilers and interpreters. The input to YACC consists of a specification
text file. The structure of the file is
definitions
%%
rules
%%
C/C++ functions
pf2

Partial preview of the text

Download Parser Generators - Compiler Construction - Lecture Notes and more Study notes Compiler Construction in PDF only on Docsity!

Sohail Aslam Compiler Construction Notes Set:2-

Le Leccttuurree 3 300

Parser Generators

Parser generators exist for LL(1) and LALR(1) grammars. For example,

  • LALR(1) - YACC, Bison, CUP
  • LL(1) – ANTLR
  • Recursive Descent - JavaCC

YACC Parser Generator

YACC – Yet Another Compiler Compiler, appeared in 1975 as a Unix application. The other companion application Lex appeared at the same time. These two greatly aided the construction of compilers and interpreters. The input to YACC consists of a specification text file. The structure of the file is

definitions %% rules %% C/C++ functions

Sohail Aslam Compiler Construction Notes Set:2-

Here, for example, is the YACC file for a calculator

%token NUMBER LPAREN RPAREN %token PLUS MINUS TIMES DIVIDE %% expr : expr PLUS expr | expr MINUS expr | expr TIMES expr | expr DIVIDE expr | LPAREN expr RPAREN | MINUS expr | NUMBER ; %%

The Flex input file for a calculator is

%{ #include "y.tab.h" %} digit [0-9] ws [ \t\n]+ %% {ws} ; {digit}+ {return NUMBER;} "+" {return PLUS;} "*" {return TIMES;} "/" {return DIVIDE;} "–" {return MINUS;} %%

The following diagram outlines the process of building a parser with YACC and Lex.

expr.y YACC

expr.l

y.tab.c

lex lex.yy.c

CC expr.exe

y.tab.h