Compiler Construction Sohail Aslam Lecture 30 Parser Generators


















![Beyond Syntax § In “a[i, j, k]” does a have three dimensions? § Does Beyond Syntax § In “a[i, j, k]” does a have three dimensions? § Does](https://slidetodoc.com/presentation_image_h2/4849778e26f0c540015a3a53b74307be/image-19.jpg)
![Beyond Syntax § In “a[i, j, k]” does a have three dimensions? § Does Beyond Syntax § In “a[i, j, k]” does a have three dimensions? § Does](https://slidetodoc.com/presentation_image_h2/4849778e26f0c540015a3a53b74307be/image-20.jpg)
![Beyond Syntax § In “a[i, j, k]” does a have three dimensions? § Does Beyond Syntax § In “a[i, j, k]” does a have three dimensions? § Does](https://slidetodoc.com/presentation_image_h2/4849778e26f0c540015a3a53b74307be/image-21.jpg)
- Slides: 21

Compiler Construction Sohail Aslam Lecture 30

Parser Generators § YACC – Yet Another Compiler appeared in 1975 as a Unix application. § The other companion application Lex appeared at the same time. 2

Parser Generators § These two greatly aided the construction of compilers and interpreters. 3

YACC Parser Generator § The input to YACC consists of a specification text file. § The structure of the file is definitions %% rules %% C/C++ functions 4

YACC Parser Generator § The input to YACC consists of a specification text file. § The structure of the file is definitions %% rules %% C/C++ functions 5

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 ; %% 6

Flex input file for a calculator %{ #include "y. tab. h" %} digit [0 -9] ws [ tn]+ %% {ws} ; {digit}+ {return NUMBER; } "+" {return PLUS; } "*" {return TIMES; } "/" {return DIVIDE; } "–" {return MINUS; } %% 7

Building a parser with YACC and Lex expr. y YACC y. tab. c y. tab. h expr. l lex CC expr. exe lex. yy. c 8

Context Sensitive Analysis

Beyond Syntax void fie(int a, int b) {. . } void fee() { int f[3], g[0], h, i, j, k; char* p; fie(h, i, “ab”); k = f*i+j; what is wrong with h = g[17]; this program? p = 10; } 10

Beyond Syntax void fie(int a, int b) {. . } void fee() { int f[3], g[1], h, i, j, k; char* p; fie(h, i, “ab”); k = f*i+j; dimension of g is 1, h = g[17]; index is 17 p = 10; } 11

Beyond Syntax void fie(int a, int b) {. . } void fee() { int f[3], g[1], h, i, j, k; char* p; fie(h, i, “ab”); k = f*i+j; h = g[17]; wrong number of p = 10; args to function fie } 12

Beyond Syntax void fie(int a, int b) {. . } void fee() { int f[3], g[1], h, i, j, k; char* p; f is an array; fie(h, i, “ab”); used without index k = f*i+j; h = g[17]; p = 10; } 13

Beyond Syntax void fie(int a, int b) {. . } void fee() { int f[3], g[1], h, i, j, k; char* p; fie(h, i, “ab”); 10 is not a character string k = f*i+j; h = g[17]; p = 10; } 14

Beyond Syntax To generate code, the compiler needs to answer many questions 15

Beyond Syntax § Is “x” a scaler, an array or a function? § Is “x” declared before it is used? § Is the expression “x*y+z” type-consistent? 16

Beyond Syntax § Is “x” a scaler, an array or a function? § Is “x” declared before it is used? § Is the expression “x*y+z” type-consistent? 17

Beyond Syntax § Is “x” a scaler, an array or a function? § Is “x” declared before it is used? § Is the expression “x*y+z” type-consistent? 18
![Beyond Syntax In ai j k does a have three dimensions Does Beyond Syntax § In “a[i, j, k]” does a have three dimensions? § Does](https://slidetodoc.com/presentation_image_h2/4849778e26f0c540015a3a53b74307be/image-19.jpg)
Beyond Syntax § In “a[i, j, k]” does a have three dimensions? § Does “*p” reference the result of “new”? § Do “p” and “q” refer to the same memory location? 19
![Beyond Syntax In ai j k does a have three dimensions Does Beyond Syntax § In “a[i, j, k]” does a have three dimensions? § Does](https://slidetodoc.com/presentation_image_h2/4849778e26f0c540015a3a53b74307be/image-20.jpg)
Beyond Syntax § In “a[i, j, k]” does a have three dimensions? § Does “*p” reference the result of “new”? § Do “p” and “q” refer to the same memory location? 20
![Beyond Syntax In ai j k does a have three dimensions Does Beyond Syntax § In “a[i, j, k]” does a have three dimensions? § Does](https://slidetodoc.com/presentation_image_h2/4849778e26f0c540015a3a53b74307be/image-21.jpg)
Beyond Syntax § In “a[i, j, k]” does a have three dimensions? § Does “*p” reference the result of “new”? § Do “p” and “q” refer to the same memory location? 21