CD Labmanual

download CD Labmanual

of 24

Transcript of CD Labmanual

  • 8/10/2019 CD Labmanual

    1/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 1

    Experiment no.1

    AIM: Write a program to implement simple lexical analyzer using C language.

    #include#include#include#includevoid keyword(char str[10]){

    if(strcmp("void",str)==0||strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||strcmp("int",str)==0||strcmp("float",str)==0||strcmp("char",str)==0||strcmp("double",str)==0||strcmp("static",str)==0||strcmp("switch",str)==0||strcmp("case",str)==0)

    printf("\n%s is a keyword",str);else printf("\n%s is an identifier",str);

    }void main(){

    FILE *f1,*f2,*f3;char c,str[10],st1[10];int num[100],lineno=0,tokenvalue=0,i=0,j=0,k=0;clrscr();

    f1=fopen("D:\\input.txt","r");f2=fopen("identifier","w");f3=fopen("specialchar","w");

    while((c=getc(f1))!=EOF){

    if(isdigit(c)){

    tokenvalue=c-'0';c=getc(f1);while(isdigit(c)){

    tokenvalue*=10+c-'0';c=getc(f1);

    }num[i++]=tokenvalue;

  • 8/10/2019 CD Labmanual

    2/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 2

    ungetc(c,f1);}else if(isalpha(c)){

    putc(c,f2);c=getc(f1);while(isdigit(c)||isalpha(c)||c=='_'||c=='$'){

    putc(c,f2);c=getc(f1);

    } putc(' ',f2);ungetc(c,f1);

    }

    else if(c==' '||c=='\t') printf(" ");else if(c=='\n')

    lineno++;else

    putc(c,f3);}fclose(f2);fclose(f3);fclose(f1);

    printf("\nThe no's in the program are");for(j=0;j

  • 8/10/2019 CD Labmanual

    3/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 3

    }}fclose(f2);f3=fopen("specialchar","r");

    printf("\nSpecial characters are");while((c=getc(f3))!=EOF)

    printf("\t%c",c); printf("\n");fclose(f3);

    printf("Total no. of lines are:%d",lineno);getch();

    }

    Input.txtvoid main(){

    int a=3;int b=5;int c;c=a+b;

    }

    Output:

  • 8/10/2019 CD Labmanual

    4/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 4

    Experiment no.2

    AIM: Write a Program to Implement NFA for regular expression (aa*)/ (bb*).

    #include#include#includevoid main(){

    charst[20];inti,count=0,len;clrscr();

    printf("\n\nEnter string");

    scanf("%s",st);len=strlen(st);if(st[0]=='a'){

    for(i=1;i

  • 8/10/2019 CD Labmanual

    5/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 5

    count++;continue;

    }else{

    count==0; break;

    }}

    }if(count == len-1){

    printf("valid string");

    }else{

    printf("invalid string");}getch();

    }

    Output:

  • 8/10/2019 CD Labmanual

    6/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 6

    Experiment no.3

    AIM: Write a Program to Implement NFA for regular expression (a/b)* c (a/b)*.

    #include#include#include

    void main(){

    int i=0,j=0,len;charst[64];

    printf("\nEnter Any String of Type (a/b)* c (a/b)*: ");scanf("%s",st);len=strlen(st);

    for(i=0;i

  • 8/10/2019 CD Labmanual

    7/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 7

    break;}

    }if(i==len){

    printf("\nYour String Shall Pass!!");goto end;

    }}

    printf("\nYour String Shall not Pass!!");end:getch();

    }

    Output:

  • 8/10/2019 CD Labmanual

    8/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 8

    Experiment no.4

    AIM: Write a Program to Implement NFA for regular expression (a/b)*abb.

    #include#include#include

    void main(){

    inti,len,n;charstr[20],s;clrscr();

    printf("\nEnter String Which is Built From Grammer (a/b)*abb:");scanf ("%s",&str);len=strlen(str);for(i=0;i

  • 8/10/2019 CD Labmanual

    9/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 9

    Output:

  • 8/10/2019 CD Labmanual

    10/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 10

    Experiment no.5

    AIM: Write a Program to Demonstrate use of LEX compiler.

    Lex Syntax and Example

    Lex is short for "lexical analysis". Lex takes an input file containing a set of lexical analysis rules orregular expressions. For output, Lex produces a C function which when invoked, finds the next match inthe input stream.

    1. Format of lex input: (beginning in col. 1) declarations

    %%token-rules%%

    aux-procedures2. Declarations:

    a) string sets; name character-class b) standard C; %{ -- c declarations --

    %}

    3. Token rules: regular-expression {optional C-code}

    a) if the expression includes a reference to a character class, enclose the class name in brackets { } b) regular expression operators;

    * , + --closure, positive closure" " or \ --protection of special chars| --or^ --beginning-of-line anchor() --grouping$ --end-of-line anchor? --zero or one. --any char (except \n){ref} --reference to a named character class (a definition)[ ] --character class[^ ] --not-character class

    4. Match rules: Longest match is preferred. If two matches are equal length, the first match is preferred. Remember, lex partitions, it does not attempt to find nested matches. Once a character becomes part of a match, it is no longer considered for other matches.

    5. Built-in variables: yytext -- ptr to the matching lexeme. ( char *yytext ;)yylen -- length of matching lexeme ( yytext ). Note: some systems use yyleng

  • 8/10/2019 CD Labmanual

    11/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 11

    6. Aux Procedures: C functions may be defined and called from the C-code of token rules or fromother functions. Each lex file should also have a yyerror() function to be called when lexencounters an error condition.

    Example header file: tokens.h

    #define NUM 1 // define constants used by lexyy.c#define ID 2 // could be defined in the lex rule file#define PLUS 3#define MULT 4#define ASGN 5#define SEMI 6

    7. Example lex file

    D [0-9] /* note these lines begin in col. 1 */A [a-zA-Z]%{#include tokens.h %}%%{D}+ return (NUM); /* match integer numbers */{A}({A}|{D})* return (ID); /* match identifiers */"+" return (PLUS); /* match the plus sign (note protection) */

    "*" return (MULT); /* match the mult sign (note protection again) */: = return (ASGN); /* match the assignment string */; return (SEMI); /* match the semi colon */. ; /* ignore any unmatched chars

    %%

    void yyerror () /* default action in case of error in yylex() */{ printf (" error\n");

    exit(0);}

    void yywrap () { } /* usually only needed for some Linux systems */

    8. Execution of lex: (to generate the yylex() function file and then compile a user program)(MS) c:> flex rulefile (Linux) $ lex rulefile

    flex produces lexyy.c lex produces lex.yy.c

    The produced .c file contains this function: int yylex()

  • 8/10/2019 CD Labmanual

    12/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 12

    9. User program:

    (The above scanner file must be linked into the project)

    #include #include tokens.h

    int yylex (); // scanner prototypeextern char* yytext;

    main (){ int n;

    while ( n = yylex() ) // call scanner until it returns 0 for EOF printf (" %d %s\n", n, yytext); // output the token code and lexeme string

    }

  • 8/10/2019 CD Labmanual

    13/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 13

    Experiment no.6

    AIM: Write a program to eliminate left recursion from a grammar.

    #include#include#include#define SIZE 10void main (){

    charnon_terminal;charbeta,alpha;char production[SIZE];

    int index=3; /* starting of the string following "->" */ printf("\n \nEnter the grammar:\n");scanf("%s",production);non_terminal=production[0];if(non_terminal==production[index]){

    alpha=production[index+1]; printf("Grammar is left recursive.\n");while(production[index]!=0 && production[index]!='|'){

    index++;if(production[index]!=0){

    beta=production[index+1]; printf("Grammar without left recursion:\n"); printf("%c->%c%c\'",non_terminal,beta,non_terminal);

    printf("\n%c\'->%c%c\'|E\n",non_terminal,alpha,non_terminal);}else

    printf("Grammar can't be reduced\n");}

    }else

    printf("Grammar is not left recursive.\n");

    getch();}

  • 8/10/2019 CD Labmanual

    14/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 14

    Output:

  • 8/10/2019 CD Labmanual

    15/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 15

    Experiment no.7

    AIM: Write a program to implement left factor a grammar.

    #include#include#includevoid main(){

    char a[10],a1[10],a2[10],a3[10],a4[10],a5[10];int i,j=0,k,l;clrscr();

    printf("enter any productions A->");gets(a);for(i=0;a[i]!='/';i++,j++)

    a1[j]=a[i];a1[j]='\0';

    for(j=++i,i=0;a[j]!='\0';j++,i++)a2[i]=a[j];a2[i]='\0';k=0;l=0;

    for(i=0;i

  • 8/10/2019 CD Labmanual

    16/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 16

    strcat(a4,a5); printf("\n A->%s",a3); printf("\n X->%s",a4);getch();

    }

    Output:

  • 8/10/2019 CD Labmanual

    17/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 17

    Experiment no.8

    AIM: Write a program that will find the FIRST SET of the grammar.

    #include#includechar array[10][20],temp[10];int c,n;void fun(int,int[]);int fun2(int i,int j,int p[],int );

    void main(){

    int p[2],i,j; printf("Enter the no. of productions :");scanf("%d",&n);

    printf("Enter the productions :\n");for(i=0;i

  • 8/10/2019 CD Labmanual

    18/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 18

    p[0]=i;p[1]=j+1;fun(k,p);return 0;

    }else{

    for(k=0;kc)return 1;else return 0;

    }

    }void fun(int i,int p[]){

    int j,k,key;for(j=2;array[i][j] != NULL; j++){

    if(array[i][j-1]=='/'){

    if(array[i][j]>= 'A' && array[i][j]='A' && array[p[0]][p[1]]

  • 8/10/2019 CD Labmanual

    19/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 19

    {if(fun2(p[0],p[1],p,key))temp[++c]=array[p[0]][p[1]];

    }}

    }}

    }}

    Output:

  • 8/10/2019 CD Labmanual

    20/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 20

    Experiment no.9

    AIM: Write a program that will find the FOLLOW SET of the grammar.

    #include#includeint n,m=0,p,i=0,j=0;char a[10][10],f[10];void follow(char c);void first(char c);void main(){

    int i,z;char c,ch;clrscr();

    printf("\nEnter the no.of productions:");scanf("%d",&n);

    printf("\nEnter the productions(epsilon=$):\n");for(i=0;i

  • 8/10/2019 CD Labmanual

    21/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 21

    {if(a[i][j]==c){

    if(a[i][j+1]!='\0')first(a[i][j+1]);if(a[i][j+1]=='\0'&&c!=a[i][0])follow(a[i][0]);

    }}

    }}void first(char c){

    int k;if(!(isupper(c)))f[m++]=c;

    for(k=0;k

  • 8/10/2019 CD Labmanual

    22/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 22

    Experiment no.10

    AIM: Write a Program to demonstrate use of YACC compiler.

    Using yacc

    1. Format for the rule file;declarations//beginning in col 1%%grammar_rules actions%%#include "lexyy.c"main() { ... } other user functions...

    A. Declarations: global declarations are made by enclosing them as the following;%{ #include int x, y;%}

    B. The start symbol of the grammar must also be declared; %start symbolname

    2. Declare all tokens to be returned by the scanner;%token ID 1 ICONST 2 RCONST 3 LBRACK 4 RBRACK 5 ... etc.

    3. Grammar rule format;

    nonterm : handle 1 { action 1 }| handle 2 { action 2 } ;

    A. Handles may contain nonterminals and tokens.B. The grammar must be complete -- all nonterminals must be defined so than the leaves of all

    parse trees contain tokens (terminals).C. Actions are C code blocks where;

    nonterminals on the left side may be assigned a value using the pseudo variable $$Example: $$ = 1;

    The values of previously-assigned nonterminals in handles can be referenced with pseudo

    variables $n (where n corresponds to the position in the handle). Example: x = $2; y =$3; yytext, yylen are globally declared in lex to access, provide a local extern declaration.

    4. Interface;

    The main program (provided by the user) should call yyparse(). This parser calls yylex() each timeit needs a new token to continue the parse. Note that lexyy.c was included prior to the main program

  • 8/10/2019 CD Labmanual

    23/24

    COMPILER DESIGN (170701)

    PROF. NIDHI GONDALIA PAGE 23

    section of the input file to yacc . This allows the definitions for tokens in the yacc input file to berecognized in yylex(). yyparse() returns 0 if successful.

    In some systems, the function yywrap() must be defined. This is normally available in UNIXlibraries but may be unknown to some MS or Apple compilers. Placing the following at the bottomof the yacc input file will suffice;

    yywrap () { return(1); };Yacc tables under MS may not be capable of handling some large grammars. The header file calledyacc.h can be edited to change the table size default. If the following is found;#define SMALLTAB YES

    It can be changed to #define HUGETAB YES or #define MEDTAB YES

    To increase the table size. A bug has been reported when using yacc under MSDOS when intrinsicC functions such as sqrt, sin, cos, etc. have been invoked.

    5. Error handling;

    It is often acceptable to stop all processing when an error is found. It may be more useful to con-tinue to find further syntax errors. If this is the case, this leads to the problem of getting the parser"started" again. To provide this utility, yacc reserves the token error.

    When an error occurs, yyerror() is called. Next, yacc will pop partially completed handles until thetoken error can be shifted onto the stack. At that point, yacc will discard input until the tokenfollowing the error token can be shifted onto the stack. The token following error is defined as thesynchronization symbol. For example;

    stmt : assgn| cond

    | loop| error SEMI { printf (" illegal statement\n"); }| error END { printf ( illegal statement \n); } ;

    In this grammar rule, if yacc is attempting to form a branch in the parse tree underneith a stmt -- butcannot, yacc would attempt to discard that all partially completed handles until the stmt can bereduced to error. All input would then be discarded up to the next SEMI or END. In other words, thisis panic recovery and the SEMI or END is the synchronization token. In other words, yacc attemptsto skip ahead to the next stmt. To prevent cascading of errors, yacc will insure that the first threetokens of the next statement are correct before proceeding. If they are not, the error action is againtaken and yacc again searches for the next SEMI or END. Naturally, other error actions can be

    defined using user-written C code, but this simple built-in mechanism is often useful.

    6. Use of Yacc with MS;

    Yacc requires a skeleton file to be in the directory \lib on the default drive -- check the Readme fileon the distribution disk. The MS version of yacc expects a single argument representing the name ofthe specification file given without an extension. Yacc will assume this file has a .y extension. The

  • 8/10/2019 CD Labmanual

    24/24

    COMPILER DESIGN (170701)

    parser constructed by yacc will have the same file name with a new .c extension. For example, if thespecification file is parser.y , then the following command line;c:> yacc parser

    Will produce parser.c which can then be compiled. Once again, be sure to use the MS model if you

    are using a windows capable IDE such as Borland or Microsoft C++. The output of yacc willgenerally produce many warning messages when compiled (which can all be ignored), but should produce no errors.

    Under UNIX, yacc produces an output file with a fixed name of y.tab.c The output file (which stillincludes the lex-generated scanner) must be linked with the "l" and "y" libraries as in the followingexample;

    % cc -o parser y.tab.c -ll -ly

    Using yacc with the -v option under UNIX will cause yacc to produce y.output containing the

    complete parsing tables and descriptions of any conflicts in the grammar.7. Yacc errors;

    The two most common errors reported by yacc are shift-reduce and reduce-reduce conflictsindicating that the grammar is inconsistent, incomplete, or at least not LR (1). If you are confidentthat the grammar is in fact LR (1), you probably left off a semicolon at the end of a grammar rule ormade some other typing mistake.