Skip to content

Instantly share code, notes, and snippets.

@rueian
Created April 16, 2015 02:53
Show Gist options
  • Save rueian/d2c19835ff54f772ec81 to your computer and use it in GitHub Desktop.
Save rueian/d2c19835ff54f772ec81 to your computer and use it in GitHub Desktop.
pb
%{
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include "y.tab.h"
void yyerror(char *);
%}
%%
s {
yylval.strval = strdup(yytext);
return STRDCL;
}
[a-o|q|r|t-z] {
yylval.strval = strdup(yytext);
return ID;
}
\" {
yylval.strval = strdup(yytext);
return QUOTE;
}
p {
yylval.strval = strdup(yytext);
return PRINT;
}
[a-z|A-Z|0-9]* {
yylval.strval = strdup(yytext);
return STRING;
}
[ \n\t] {}
. {
yyerror("error");
exit(0);
}
%%
int yywrap(void) {
return 1;
}
%{
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
%}
%token STRDCL ID QUOTE STRING PRINT
%union {
char * strval;
}
%type <strval> STRDCL ID QUOTE STRING PRINT
%type <strval> dcl stmt astring
%{
void yyerror(char *);
int yylex(void);
%}
%%
proc: dcl stmt {
printf("%s", $1);
printf("%s", $2);
}
|
;
dcl: STRDCL ID astring {
sprintf($$, "strdcl %s\nid %s\n%s", $1, $2, $3);
}
|
;
astring: QUOTE STRING QUOTE {
sprintf($$, "quote \"\nstring %s\nquote \"\n", $2);
}
|
;
stmt: PRINT ID {
sprintf($$, "print p\nid %s\n", $2);
}
|
;
%%
void yyerror(char *s) {
printf("valid input");
}
int main(void) {
yyparse();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment