├── .gitignore ├── LICENSE ├── README.md ├── calc.l ├── calc.y └── makefile /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | 5 | # Libraries 6 | *.lib 7 | *.a 8 | 9 | # Shared objects (inc. Windows DLLs) 10 | *.dll 11 | *.so 12 | *.so.* 13 | *.dylib 14 | 15 | # Executables 16 | *.exe 17 | *.out 18 | *.app 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Jonathan Engelsma 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | yacc-tutorial 2 | ============= 3 | 4 | This is the source code for my Yacc/Bison screencast tutorial on [YouTube](https://youtu.be/__-wUHG2rfM). 5 | -------------------------------------------------------------------------------- /calc.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "y.tab.h" 3 | void yyerror (char *s); 4 | int yylex(); 5 | %} 6 | %% 7 | "print" {return print;} 8 | "exit" {return exit_command;} 9 | [a-zA-Z] {yylval.id = yytext[0]; return identifier;} 10 | [0-9]+ {yylval.num = atoi(yytext); return number;} 11 | [ \t\n] ; 12 | [-+=;] {return yytext[0];} 13 | . {ECHO; yyerror ("unexpected character");} 14 | 15 | %% 16 | int yywrap (void) {return 1;} 17 | 18 | -------------------------------------------------------------------------------- /calc.y: -------------------------------------------------------------------------------- 1 | %{ 2 | void yyerror (char *s); 3 | int yylex(); 4 | #include /* C declarations used in actions */ 5 | #include 6 | #include 7 | int symbols[52]; 8 | int symbolVal(char symbol); 9 | void updateSymbolVal(char symbol, int val); 10 | %} 11 | 12 | %union {int num; char id;} /* Yacc definitions */ 13 | %start line 14 | %token print 15 | %token exit_command 16 | %token number 17 | %token identifier 18 | %type line exp term 19 | %type assignment 20 | 21 | %% 22 | 23 | /* descriptions of expected inputs corresponding actions (in C) */ 24 | 25 | line : assignment ';' {;} 26 | | exit_command ';' {exit(EXIT_SUCCESS);} 27 | | print exp ';' {printf("Printing %d\n", $2);} 28 | | line assignment ';' {;} 29 | | line print exp ';' {printf("Printing %d\n", $3);} 30 | | line exit_command ';' {exit(EXIT_SUCCESS);} 31 | ; 32 | 33 | assignment : identifier '=' exp { updateSymbolVal($1,$3); } 34 | ; 35 | exp : term {$$ = $1;} 36 | | exp '+' term {$$ = $1 + $3;} 37 | | exp '-' term {$$ = $1 - $3;} 38 | ; 39 | term : number {$$ = $1;} 40 | | identifier {$$ = symbolVal($1);} 41 | ; 42 | 43 | %% /* C code */ 44 | 45 | int computeSymbolIndex(char token) 46 | { 47 | int idx = -1; 48 | if(islower(token)) { 49 | idx = token - 'a' + 26; 50 | } else if(isupper(token)) { 51 | idx = token - 'A'; 52 | } 53 | return idx; 54 | } 55 | 56 | /* returns the value of a given symbol */ 57 | int symbolVal(char symbol) 58 | { 59 | int bucket = computeSymbolIndex(symbol); 60 | return symbols[bucket]; 61 | } 62 | 63 | /* updates the value of a given symbol */ 64 | void updateSymbolVal(char symbol, int val) 65 | { 66 | int bucket = computeSymbolIndex(symbol); 67 | symbols[bucket] = val; 68 | } 69 | 70 | int main (void) { 71 | /* init symbol table */ 72 | int i; 73 | for(i=0; i<52; i++) { 74 | symbols[i] = 0; 75 | } 76 | 77 | return yyparse ( ); 78 | } 79 | 80 | void yyerror (char *s) {fprintf (stderr, "%s\n", s);} 81 | 82 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | calc: lex.yy.c y.tab.c 2 | gcc -g lex.yy.c y.tab.c -o calc 3 | 4 | lex.yy.c: y.tab.c calc.l 5 | lex calc.l 6 | 7 | y.tab.c: calc.y 8 | yacc -d calc.y 9 | 10 | clean: 11 | rm -rf lex.yy.c y.tab.c y.tab.h calc calc.dSYM 12 | 13 | --------------------------------------------------------------------------------