Docsity

Parser Generators - Compiler Construction - Lecture Slides, Slides of Compiler Construction

Partial preview of the text

Download Parser Generators - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Compiler

Construction

Lecture 30

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.

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 Parser Generator

 The input to YACC consists of a
specification text file.
 The structure of the file is
definitions
rules
C/C++ functions

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

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

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

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

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

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

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?

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

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?