Basic predefined lex variables and their meaning in lex

Image

Advertisements

One thought on “Basic predefined lex variables and their meaning in lex

  1. NESTED LOOP
    FLEX FILE:
    %{

    #include “nestfor.tab.h”
    #include

    %}

    %%

    [0-9]+ {
    yylval=atoi(yytext);

    return NUM;
    }

    “for” { return KEYWORD;}

    “int” { return KEYWORD;}

    “printf” { return KEYWORD;}

    “>=”|”<="|"=="|"!="|"” { yylval = *yytext;

    return RELOP;
    }

    “++”|”–” { yylval=*yytext;

    return INC;

    }

    “{” { return OPENBRACE; }

    “}” { return CLOSEBRACE;}

    [a-zA-Z]+ { yylval = *yytext;

    return ID;
    }

    [=();\n] return *yytext;

    [\t]

    . yyerror (“INVALID INPUT”);

    %%

    int yywrap(void)
    {

    return 1;

    }

    YACC FILE :

    %{

    #include
    void yyerror(char*s);

    %}

    %token ID KEYWORD RELOP INC NUM OPENBRACE CLOSEBRACE

    %%

    program:
    program Statement’\n’
    |
    ;

    Statement:project {printf(“SYNTAX ACCEPTED\n”);}
    project : KEYWORD ‘(‘ E1 ‘;’ E2 ‘;’ E3 ‘)’ ‘\n’ OPENBRACE’\n’ KEYWORD ‘(‘ E1 ‘;’ E2 ‘;’ E3 ‘)’ ‘\n’ OPENBRACE’\n’ KEYWORD ‘(‘ E1 ‘;’ E2 ‘;’ E3 ‘)’ ‘\n’ CLOSEBRACE ‘\n’ CLOSEBRACE ‘\n’ CLOSEBRACE

    E1 :ID’=’ID
    |ID’=’NUM
    ;

    E2 :ID RELOP ID
    |ID RELOP NUM
    ;

    E3 :ID INC
    ;

    %%

    void yyerror(char*s) {

    printf(“%s\n”,s);

    }

    int main(void) {

    yyparse();
    return 1;

    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s