Skip to content

Instantly share code, notes, and snippets.

@meldsza
Created February 25, 2020 09:49
Show Gist options
  • Select an option

  • Save meldsza/856d9a07abc81e28c55028345010079a to your computer and use it in GitHub Desktop.

Select an option

Save meldsza/856d9a07abc81e28c55028345010079a to your computer and use it in GitHub Desktop.

Revisions

  1. meldsza created this gist Feb 25, 2020.
    16 changes: 16 additions & 0 deletions id.l
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    %{
    #include "y.tab.h"
    %}

    %%
    [0-9] {return DIGIT;}
    [a-zA-Z] {return LETTER;}
    [_] {return UND;}
    \n {return NL;}
    . {return yytext[0];}
    %%

    int yywrap()
    {
    return 1;
    }
    27 changes: 27 additions & 0 deletions id.y
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    %{
    #include <stdio.h>
    #include <stdlib.h>
    %}

    %token NL DIGIT LETTER UND
    %%
    id: letter_und alphanum NL { printf("Valid\n");exit(0);}
    ;
    letter_und: LETTER | UND
    alphanum_: LETTER|UND|DIGIT
    ;
    alphanum: alphanum_ alphanum | alphanum_
    ;
    %%
    int yyerror(char *msg)
    {
    printf("Invalid statements\n");
    exit(0);
    }

    int main()
    {
    printf("Enter input: \n");
    yyparse();
    return 0;
    }
    18 changes: 18 additions & 0 deletions nested_if.l
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    %{
    #include "y.tab.h"
    %}

    %%
    "if" {return IF;}
    [sS][0-9]* {return S;}
    [a-zA-Z][a-zA-Z0-9]* {return ID;}
    [0-9]+ {return NUM;}
    ">"|"<"|">="|"<="|"=="|"!=" {return RELOP;}
    \n {return NL;}
    . {return yytext[0];}
    %%

    int yywrap()
    {
    return 1;
    }
    30 changes: 30 additions & 0 deletions nested_if.y
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    %{
    #include <stdio.h>
    #include <stdlib.h>
    int count;
    %}

    %token NL S ID NUM IF RELOP
    %%
    stmt : ifstmt NL {printf("valid \t"); printf("Number of nested statements is : %d\n",count);exit(0);};
    ifstmt : IF'('cond')''{'ifstmt'}' {count++;}
    | S |
    ;

    cond : X RELOP X
    ;
    X : ID | NUM
    ;
    %%
    int yyerror(char *msg)
    {
    printf("Invalid statement");
    exit(0);
    }

    int main()
    {
    printf("Enter input: \n");
    yyparse();
    return 0;
    }