Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler Construction Sohail Aslam Lecture 30. 2 Parser Generators  YACC – Yet Another Compiler Compiler appeared in 1975 as a Unix application.  The.

Similar presentations


Presentation on theme: "Compiler Construction Sohail Aslam Lecture 30. 2 Parser Generators  YACC – Yet Another Compiler Compiler appeared in 1975 as a Unix application.  The."— Presentation transcript:

1 Compiler Construction Sohail Aslam Lecture 30

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

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

4 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 5 YACC Parser Generator  The input to YACC consists of a specification text file.  The structure of the file is definitions % rules % C/C++ functions

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

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

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

9 Context Sensitive Analysis

10 10 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; h = g[17]; p = 10; } what is wrong with this program?

11 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]; p = 10; } dimension of g is 1, index is 17

12 12 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]; p = 10; } wrong number of args to function fie

13 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”); k = f*i+j; h = g[17]; p = 10; } f is an array; used without index

14 14 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]; p = 10; } 10 is not a character string

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

16 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 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 18 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?

19 19 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 20 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 21 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?


Download ppt "Compiler Construction Sohail Aslam Lecture 30. 2 Parser Generators  YACC – Yet Another Compiler Compiler appeared in 1975 as a Unix application.  The."

Similar presentations


Ads by Google