Add https://github.com/gautamkmr/Compiler-Lab- examples
authorNick Downing <downing.nick@gmail.com>
Sat, 21 Jul 2018 11:50:24 +0000 (21:50 +1000)
committerNick Downing <downing.nick@gmail.com>
Sat, 21 Jul 2018 11:50:24 +0000 (21:50 +1000)
20 files changed:
.gitignore
lex-yacc-examples/Makefile [new file with mode: 0644]
lex-yacc-examples/example1.compile [new file with mode: 0644]
lex-yacc-examples/example1.l [new file with mode: 0644]
lex-yacc-examples/example2.compile [new file with mode: 0644]
lex-yacc-examples/example2.l [new file with mode: 0644]
lex-yacc-examples/example3.compile [new file with mode: 0644]
lex-yacc-examples/example3.l [new file with mode: 0644]
lex-yacc-examples/example4.compile [new file with mode: 0644]
lex-yacc-examples/example4.l [new file with mode: 0644]
lex-yacc-examples/example4.y [new file with mode: 0644]
lex-yacc-examples/example5.compile [new file with mode: 0644]
lex-yacc-examples/example5.l [new file with mode: 0644]
lex-yacc-examples/example5.y [new file with mode: 0644]
lex-yacc-examples/example6.compile [new file with mode: 0644]
lex-yacc-examples/example6.l [new file with mode: 0644]
lex-yacc-examples/example6.y [new file with mode: 0644]
lex-yacc-examples/example7.compile [new file with mode: 0644]
lex-yacc-examples/example7.l [new file with mode: 0644]
lex-yacc-examples/example7.y [new file with mode: 0644]

index 0843d67..f95c9df 100644 (file)
@@ -1,9 +1,15 @@
 __pycache__
+lex-yacc-examples/*.c
+lex-yacc-examples/*.h
+lex-yacc-examples/*.o
+lex-yacc-examples/*.xml
+lex-yacc-examples/example4
+lex-yacc-examples/example7
+skel/lex.yy.c
+skel/lex.yy.c.orig
 tests/*.c
 tests/*.o
 tests/*.xml
 tests/cal
 tests/flex0
 tests/flex1
-skel/lex.yy.c
-skel/lex.yy.c.orig
diff --git a/lex-yacc-examples/Makefile b/lex-yacc-examples/Makefile
new file mode 100644 (file)
index 0000000..ee0a250
--- /dev/null
@@ -0,0 +1,41 @@
+all: example4 example7
+
+# example4 program
+example4: example4.tab.o example4.yy.o
+       ${CC} -o $@ $^
+
+example4.tab.o: example4.tab.c
+
+example4.tab.c: example4.y
+       ../../bootstrap_bison.git/src/bison -dy $< 2>$<.xml
+       mv y.tab.h example4.tab.h
+       mv y.tab.c $@
+
+example4.yy.o: example4.yy.c
+
+example4.yy.c: example4.l
+       ../../bootstrap_flex.git/src/flex -o /dev/null $< 2>$<.xml
+       ../bootstrap_plex.py $<.xml
+       mv lex.yy.c $@
+
+# example7 program
+example7: example7.tab.o example7.yy.o
+       ${CC} -o $@ $^
+
+example7.tab.o: example7.tab.c
+
+example7.tab.c: example7.y
+       ../../bootstrap_bison.git/src/bison -dy $< 2>$<.xml
+       mv y.tab.h example7.tab.h
+       mv y.tab.c $@
+
+example7.yy.o: example7.yy.c
+
+example7.yy.c: example7.l
+       ../../bootstrap_flex.git/src/flex -o /dev/null $< 2>$<.xml
+       ../bootstrap_plex.py $<.xml
+       mv lex.yy.c $@
+
+# other
+clean:
+       rm -f *.c *.o *.xml example4 example7
diff --git a/lex-yacc-examples/example1.compile b/lex-yacc-examples/example1.compile
new file mode 100644 (file)
index 0000000..a86d44b
--- /dev/null
@@ -0,0 +1,2 @@
+lex example1.l
+cc lex.yy.c -o example1 -ll
diff --git a/lex-yacc-examples/example1.l b/lex-yacc-examples/example1.l
new file mode 100644 (file)
index 0000000..234f412
--- /dev/null
@@ -0,0 +1,8 @@
+%{
+#include <stdio.h>
+%}
+
+%%
+stop   printf("Stop command received\n");
+start  printf("Start command received\n");
+%%
diff --git a/lex-yacc-examples/example2.compile b/lex-yacc-examples/example2.compile
new file mode 100644 (file)
index 0000000..c1fb80c
--- /dev/null
@@ -0,0 +1,2 @@
+lex example2.l
+cc lex.yy.c -o example2 -ll
diff --git a/lex-yacc-examples/example2.l b/lex-yacc-examples/example2.l
new file mode 100644 (file)
index 0000000..2528382
--- /dev/null
@@ -0,0 +1,8 @@
+%{
+#include <stdio.h>
+%}
+
+%%
+[0123456789]+          printf("NUMBER\n");
+[a-zA-Z][a-zA-Z0-9]*   printf("WORD\n");
+%%
diff --git a/lex-yacc-examples/example3.compile b/lex-yacc-examples/example3.compile
new file mode 100644 (file)
index 0000000..ff734a2
--- /dev/null
@@ -0,0 +1,2 @@
+lex example3.l
+cc lex.yy.c -o example3 -ll
diff --git a/lex-yacc-examples/example3.l b/lex-yacc-examples/example3.l
new file mode 100644 (file)
index 0000000..f2784fe
--- /dev/null
@@ -0,0 +1,14 @@
+%{
+#include <stdio.h>
+%}
+
+%%
+[a-zA-Z][a-zA-Z0-9]*    printf("WORD ");
+[a-zA-Z0-9\/.-]+        printf("FILENAME ");
+\"                      printf("QUOTE ");
+\{                      printf("OBRACE ");
+\}                      printf("EBRACE ");
+;                       printf("SEMICOLON ");
+\n                      printf("\n");
+[ \t]+                  /* ignore whitespace */;
+%%
diff --git a/lex-yacc-examples/example4.compile b/lex-yacc-examples/example4.compile
new file mode 100644 (file)
index 0000000..304d3ae
--- /dev/null
@@ -0,0 +1,3 @@
+lex example4.l
+yacc -d example4.y
+cc lex.yy.c y.tab.c -o example4 
diff --git a/lex-yacc-examples/example4.l b/lex-yacc-examples/example4.l
new file mode 100644 (file)
index 0000000..d445ab8
--- /dev/null
@@ -0,0 +1,13 @@
+%{
+#include <stdio.h>
+#include "example4.tab.h" /*"y.tab.h"*/
+%}
+%%
+[0-9]+                  return NUMBER;
+heat                    return TOKHEAT;
+on|off                  return STATE;
+target                  return TOKTARGET;
+temperature             return TOKTEMPERATURE;
+\n                      /* ignore end of line */;
+[ \t]+                  /* ignore whitespace */;
+%%
diff --git a/lex-yacc-examples/example4.y b/lex-yacc-examples/example4.y
new file mode 100644 (file)
index 0000000..f4b1a49
--- /dev/null
@@ -0,0 +1,50 @@
+%{
+#include <stdio.h>
+#include <string.h>
+
+void yyerror(const char *str)
+{
+       fprintf(stderr,"error: %s\n",str);
+}
+
+int yywrap()
+{
+       return 1;
+}
+
+main()
+{
+       yyparse();
+}
+
+%}
+
+%token NUMBER TOKHEAT STATE TOKTARGET TOKTEMPERATURE
+
+%%
+
+commands: /* empty */
+       | commands command
+       ;
+
+
+command:
+       heat_switch
+       |
+       target_set
+       ;
+
+heat_switch:
+       TOKHEAT STATE 
+       {
+               printf("\tHeat turned on or off\n");
+       }
+       ;
+
+target_set:
+       TOKTARGET TOKTEMPERATURE NUMBER
+       {
+               printf("\tTemperature set\n");
+       }
+       ;
+
diff --git a/lex-yacc-examples/example5.compile b/lex-yacc-examples/example5.compile
new file mode 100644 (file)
index 0000000..4d8edb1
--- /dev/null
@@ -0,0 +1,3 @@
+lex example5.l
+yacc -d example5.y
+cc lex.yy.c y.tab.c -o example5 
diff --git a/lex-yacc-examples/example5.l b/lex-yacc-examples/example5.l
new file mode 100644 (file)
index 0000000..59b942d
--- /dev/null
@@ -0,0 +1,13 @@
+%{
+#include <stdio.h>
+#include "example5.tab.h" /*"y.tab.h"*/
+%}
+%%
+[0-9]+                  yylval=atoi(yytext); return NUMBER;
+heat                    return TOKHEAT;
+on|off                  yylval=!strcmp(yytext,"on"); return STATE;
+target                  return TOKTARGET;
+temperature             return TOKTEMPERATURE;
+\n                      /* ignore end of line */;
+[ \t]+                  /* ignore whitespace */;
+%%
diff --git a/lex-yacc-examples/example5.y b/lex-yacc-examples/example5.y
new file mode 100644 (file)
index 0000000..65ba2f7
--- /dev/null
@@ -0,0 +1,53 @@
+%{
+#include <stdio.h>
+#include <string.h>
+
+void yyerror(const char *str)
+{
+       fprintf(stderr,"error: %s\n",str);
+}
+
+int yywrap()
+{
+       return 1;
+}
+
+main()
+{
+       yyparse();
+}
+
+%}
+
+%token NUMBER TOKHEAT STATE TOKTARGET TOKTEMPERATURE
+
+%%
+
+commands:
+       | commands command
+       ;
+
+
+command:
+       heat_switch
+       |
+       target_set
+       ;
+
+heat_switch:
+       TOKHEAT STATE 
+       {
+               if($2)
+                       printf("\tHeat turned on\n");
+               else
+                       printf("\tHeat turned off\n");
+       }
+       ;
+
+target_set:
+       TOKTARGET TOKTEMPERATURE NUMBER
+       {
+               printf("\tTemperature set to %d\n",$3);
+       }
+       ;
+
diff --git a/lex-yacc-examples/example6.compile b/lex-yacc-examples/example6.compile
new file mode 100644 (file)
index 0000000..d4601d4
--- /dev/null
@@ -0,0 +1,3 @@
+lex example6.l
+yacc --verbose --debug -d example6.y
+cc lex.yy.c y.tab.c -o example6 
diff --git a/lex-yacc-examples/example6.l b/lex-yacc-examples/example6.l
new file mode 100644 (file)
index 0000000..4bd41e6
--- /dev/null
@@ -0,0 +1,18 @@
+%{
+#include <stdio.h>
+#include "example6.tab.h" /*"y.tab.h"*/
+%}
+
+%%
+
+zone                   return ZONETOK;
+file                   return FILETOK;
+[a-zA-Z][a-zA-Z0-9]*    yylval=strdup(yytext); return WORD;
+[a-zA-Z0-9\/.-]+        yylval=strdup(yytext); return FILENAME;
+\"                      return QUOTE;
+\{                      return OBRACE;
+\}                      return EBRACE;
+;                       return SEMICOLON;
+\n                      /* ignore EOL */;
+[ \t]+                  /* ignore whitespace */;
+%%
diff --git a/lex-yacc-examples/example6.y b/lex-yacc-examples/example6.y
new file mode 100644 (file)
index 0000000..aa9b67c
--- /dev/null
@@ -0,0 +1,79 @@
+%{
+#include <stdio.h>
+#include <string.h>
+
+#define YYSTYPE char *
+
+int yydebug=0;
+
+void yyerror(const char *str)
+{
+       fprintf(stderr,"error: %s\n",str);
+}
+
+int yywrap()
+{
+       return 1;
+}
+
+main()
+{
+       yyparse();
+}
+
+%}
+
+%token WORD FILENAME QUOTE OBRACE EBRACE SEMICOLON ZONETOK FILETOK
+
+%%
+
+commands:
+       |        
+       commands command SEMICOLON
+       ;
+
+
+command:
+       zone_set 
+       ;
+
+zone_set:
+       ZONETOK quotedname zonecontent
+       {
+               printf("Complete zone for '%s' found\n",$2);
+       }
+       ;
+
+zonecontent:
+       OBRACE zonestatements EBRACE 
+
+quotedname:
+       QUOTE FILENAME QUOTE
+       {
+               $$=$2;
+       }
+       ;
+
+zonestatements:
+       |
+       zonestatements zonestatement SEMICOLON
+       ;
+
+zonestatement:
+       statements
+       |
+       FILETOK quotedname 
+       {
+               printf("A zonefile name '%s' was encountered\n", $2);
+       }
+       ;
+
+block: 
+       OBRACE zonestatements EBRACE SEMICOLON
+       ;
+
+statements:
+       | statements statement
+       ;
+
+statement: WORD | block | quotedname
diff --git a/lex-yacc-examples/example7.compile b/lex-yacc-examples/example7.compile
new file mode 100644 (file)
index 0000000..b2b9129
--- /dev/null
@@ -0,0 +1,3 @@
+lex example7.l
+yacc -d example7.y
+cc lex.yy.c y.tab.c -o example7 
diff --git a/lex-yacc-examples/example7.l b/lex-yacc-examples/example7.l
new file mode 100644 (file)
index 0000000..f4bc9ae
--- /dev/null
@@ -0,0 +1,16 @@
+%{
+#include <stdio.h>
+#include <string.h>
+#include "example7.tab.h" /*"y.tab.h"*/
+%}
+%%
+[0-9]+                  yylval.number=atoi(yytext); return NUMBER;
+heater                 return TOKHEATER;
+heat                    return TOKHEAT;
+on|off                  yylval.number=!strcmp(yytext,"on"); return STATE;
+target                  return TOKTARGET;
+temperature             return TOKTEMPERATURE;
+[a-z0-9]+              yylval.string=strdup(yytext);return WORD;
+\n                      /* ignore end of line */;
+[ \t]+                  /* ignore whitespace */;
+%%
diff --git a/lex-yacc-examples/example7.y b/lex-yacc-examples/example7.y
new file mode 100644 (file)
index 0000000..bf8566f
--- /dev/null
@@ -0,0 +1,69 @@
+%{
+#include <stdio.h>
+#include <string.h>
+
+void yyerror(const char *str)
+{
+       fprintf(stderr,"error: %s\n",str);
+}
+
+int yywrap()
+{
+       return 1;
+}
+
+main()
+{
+       yyparse();
+}
+
+char *heater="default";
+
+%}
+
+%token TOKHEATER TOKHEAT TOKTARGET TOKTEMPERATURE
+
+%union 
+{
+       int number;
+       char *string;
+}
+
+%token <number> STATE
+%token <number> NUMBER
+%token <string> WORD
+
+%%
+
+commands:
+       | commands command
+       ;
+
+
+command:
+       heat_switch | target_set | heater_select
+
+heat_switch:
+       TOKHEAT STATE 
+       {
+               if($2)
+                       printf("\tHeater '%s' turned on\n", heater);
+               else
+                       printf("\tHeat '%s' turned off\n", heater);
+       }
+       ;
+
+target_set:
+       TOKTARGET TOKTEMPERATURE NUMBER
+       {
+               printf("\tHeater '%s' temperature set to %d\n",heater, $3);
+       }
+       ;
+
+heater_select:
+       TOKHEATER WORD
+       {
+               printf("\tSelected heater '%s'\n",$2);
+               heater=$2;
+       }
+       ;
\ No newline at end of file