Skip to content

Instantly share code, notes, and snippets.

@thunderInfy
Created March 15, 2020 22:04
Show Gist options
  • Select an option

  • Save thunderInfy/3c5c69fd9e847fa4a56a7d11a143d387 to your computer and use it in GitHub Desktop.

Select an option

Save thunderInfy/3c5c69fd9e847fa4a56a7d11a143d387 to your computer and use it in GitHub Desktop.
%{
#include <stdio.h>
#include <string.h>
int yylex();
int yyerror(char *s);
%}
%token STRING NUMBER SEMICOLON OTHER
%type <name> STRING
%union{
char name[20];
}
%%
program:
statements
;
statements:
| statement SEMICOLON statements
;
statement:
STRING {
if(strcmp($1,"hello")==0){
printf("Hello to you too! How're you?");
}
else{
printf("I know what you typed is a string but all I know is \"hello\" :(");
}
}
| NUMBER {
printf("This is a number");
}
| OTHER {
printf("Sorry, I can't comprehend this :(");
}
;
%%
int yyerror(char *s){
printf("Syntax error\n");
return 0;
}
int main(){
yyparse();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment