├── .gitignore ├── LICENSE ├── README.md └── src ├── c ├── 11_a.c ├── 11_b.c ├── 11_c.c ├── 11_d.c └── 11_e.c ├── lex ├── 01.l ├── 02.l ├── 03.l ├── 04.l ├── 05.l ├── 06.l ├── 07.l ├── 08.l ├── 09.l ├── 10.l ├── 12.l ├── 13.l ├── 14.l ├── 16.l ├── 17.l ├── 18.l ├── 19.l ├── 20.l ├── 21.l ├── 22.l └── 23.l └── yacc ├── binaryToDecimal ├── binToDec.l ├── binToDec.y └── output.png ├── calculator ├── calc.l ├── calc.y └── output.png ├── contextFreeGrammar ├── cfg.l ├── cfg.y └── output.png ├── evaluation ├── evaluation.l ├── evaluation.y └── output.png ├── infixToPostfix ├── inToPost.l ├── inToPost.y └── output.png ├── infixToPrefix ├── inToPre.l ├── inToPre.y └── output.png ├── palindrome ├── output.png ├── palindrome.l └── palindrome.y ├── startAndEndString ├── output.png ├── start.l └── start.y ├── string ├── output.png ├── string.l └── string.y └── variableDetector ├── output.png ├── variable.l └── variable.y /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore Mac system files 2 | .DS_store 3 | 4 | # Ignore all text files 5 | *.txt 6 | 7 | # Ignore output files 8 | out 9 | a.out 10 | 11 | # Ignore lex files 12 | lex.yy.c 13 | 14 | # Ignore all yacc files 15 | y.tab.c 16 | y.tab.h -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Debojeet Das 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 | # compiler-programs 2 | 3 | **This Repository contains C, LEX and YACC files made during our couse of Compiler Design (CS601).** 4 | 5 | ## Pre-Requisites 6 | 7 | Following package are needed for running the c, lex and yacc files. Please run the following commands: 8 | 9 | Run the update command (just in case) 10 | ```bash 11 | sudo apt-get update 12 | ``` 13 | then: 14 | ```bash 15 | sudo apt-get install gcc 16 | sudo apt-get install lex 17 | sudo apt-get install bison 18 | ``` 19 | or instead of lex you can install flex using 20 | ```bash 21 | sudo apt-get install flex 22 | ``` 23 | 24 | ## Usage 25 | For compiling the C files: 26 | ```sh 27 | gcc 28 | ./a.out 29 | ``` 30 | For compiling the lex files: 31 | ```sh 32 | lex or flex 33 | gcc lex.yy.c 34 | ./a.out 35 | ``` 36 | For compiling the yacc files (Along with the lex files): 37 | ```sh 38 | yacc -d 39 | lex 40 | gcc lex.yy.c y.tab.c 41 | ./a.out 42 | ``` 43 | 44 | ## Contents 45 | 46 | ### 1. C Programs 47 | A finite automata is implemented using C programming language which can detect: 48 | - [Unsigned numbers](./src/c/11_a.c) 49 | - [Integers](src/c/11_b.c) 50 | - [Real numbers](src/c/11_c.c) 51 | - [Identifiers](src/c/11_d.c) 52 | - [Relational operators](src/c/11_e.c) 53 | 54 | ### 2. Lex Programs 55 | - [Lex program that copies a file, replacing each nonempty sequence of white space by a single blank.](./src/lex/01.l) 56 | - [Lex program that copies a C program, replacing each instance of the keyword float with double.](./src/lex/02.l) 57 | - [Lex program which will modify the words in the following way:](./src/lex/03.l) 58 | - [If the first letter is a consonant, move it to the end of the word and then add ay.](./src/lex/03.l) 59 | - [If the first letter is a vowel, just add ay to the end of the word.](./src/lex/03.l) 60 | - [Lex program to identify identifiers from a given input source file.](./src/lex/04.l) 61 | - [Lex program to count the number of vowels and consonants from an input file and write the results to a file.](./src/lex/05.l) 62 | - [Lex program to count the number of lines in a given input source file.](./src/lex/06.l) 63 | - [Lex program to count the number of comment lines in a c program. Also eliminate that comment line. (input read from file).](./src/lex/07.l) 64 | - [Lex program to identify correctly an inputted 8 bit binary number.](./src/lex/08.l) 65 | - [Lex program to find out all the tokens from input C program.(input read from file)](./src/lex/09.l) 66 | - [Lex program to count the number of tokens and find out all the tokens from input C program. (input read from file)](./src/lex/10.l) 67 | - [Lex program to identify all the numbers in an input text and then perform the sum of the numbers.](./src/lex/12.l) 68 | - [Lex program to count the number of the word “the” in an input text.](./src/lex/13.l) 69 | - [Lex program to count the number of stopwords and remove all of them.](./src/lex/14.l) 70 | - [Lex program to design a simple calculator.](./src/lex/16.l) 71 | - [Lex program to count the number of palindromes present in a input text and write them to a separate text file.](./src/lex/17.l) 72 | - [Lex program to check valid arithmetic expressions.](./src/lex/18.l) 73 | - [Lex program to find the length of an input text.](./src/lex/19.l) 74 | - [Lex program to reverse all the words in an input text.](./src/lex/20.l) 75 | - [Lex program to find the smallest word in an input text.](./src/lex/21.l) 76 | - [Lex program to convert lowercase characters to uppercase.](./src/lex/22.l) 77 | - [Lex program to sort all the words in an input text as per their length.](./src/lex/23.l ) 78 | 79 | ### 3. Yacc Programs 80 | - [YACC program to recognize strings of { anbb | n≥5 }.](./src/yacc/string) 81 | - [YACC program for Binary to Decimal Conversion.](./src/yacc/binaryToDecimal) 82 | - [YACC program to recognize string with grammar { bnan | n≥0 }.](./src/yacc/contextFreeGrammar) 83 | - [Yacc Program to evaluate a given arithmetic expression.](./src/yacc/evaluation) 84 | - [YACC program to check whether given string is Palindrome or not.](./src/yacc/palindrome) 85 | - [YACC program for Conversion of Infix to Postfix expression.](./src/yacc/infixToPostfix) 86 | - [YACC program for Conversion of Infix to Prefix expression.](./src/yacc/infixToPrefix) 87 | - [YACC program which accept strings that starts with 0 and ends with 1.](./src/yacc/startAndEndString) 88 | - [YACC program to implement a Calculator and recognize a valid Arithmetic expression.](./src/yacc/calculator) 89 | - [YACC program to recognize a valid variable, which starts with a letter, followed by any number of letters or digits.](./src/yacc/variableDetector) 90 | 91 | ## Contributing 92 | Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. 93 | 94 | Please make sure to update tests as appropriate. 95 | 96 | 1. Fork it! 97 | 2. Create your feature branch: `git checkout -b my-new-feature` 98 | 3. Commit your changes: `git commit -am 'Add some feature'` 99 | 4. Push to the branch: `git push origin my-new-feature` 100 | 5. Submit a pull request :D 101 | 102 | ## :+1: Credits 103 | 104 | - [Debojeet Das](https://rickydebojeet.github.io) 105 | - [Swaubhik Chakraborty](https://www.swaubhik.engineer/) 106 | 107 | 108 | ## License 109 | [MIT](https://choosealicense.com/licenses/mit/) 110 | -------------------------------------------------------------------------------- /src/c/11_a.c: -------------------------------------------------------------------------------- 1 | /* C program to implement the transition diagram for unsigned numbers */ 2 | 3 | #include 4 | #include 5 | 6 | /* Use a DFA approach. state indicates the state of the DFA. */ 7 | enum Statetype {NORMAL, NUM, END}; 8 | 9 | enum Statetype start(int); 10 | enum Statetype state1(int); 11 | 12 | 13 | /*------------------------------------------------------------*/ 14 | /* main: Read text from stdin. Check for the unsigned numbers */ 15 | /* and return the result to stdout. Return 0 */ 16 | /*------------------------------------------------------------*/ 17 | int main() 18 | { 19 | int c; 20 | enum Statetype state = NORMAL; 21 | for( ; ; ) { 22 | c = getchar(); 23 | if (c == EOF) 24 | break; 25 | switch (state) 26 | { 27 | case NORMAL: 28 | state = start(c); 29 | break; 30 | case NUM: 31 | state = state1(c); 32 | break; 33 | case END: 34 | printf("Unsigned number detected.\n"); 35 | return 0; 36 | } 37 | } 38 | return 0; 39 | } 40 | 41 | /*------------------------------------------------------------*/ 42 | /* start: Implement the state 0 of the DFA. c is the current */ 43 | /* DFA character. Return the next state. */ 44 | /*------------------------------------------------------------*/ 45 | enum Statetype start(int c) 46 | { 47 | enum Statetype state; 48 | if(isdigit(c)) 49 | state = NUM; 50 | return state; 51 | } 52 | 53 | /*------------------------------------------------------------*/ 54 | /* state1: Implement the state 1 of the DFA. c is the current */ 55 | /* DFA character. Return the next state. */ 56 | /*------------------------------------------------------------*/ 57 | enum Statetype state1(int c) 58 | { 59 | enum Statetype state; 60 | if (isdigit(c)) 61 | state = NUM; 62 | else 63 | state = END; 64 | return state; 65 | } -------------------------------------------------------------------------------- /src/c/11_b.c: -------------------------------------------------------------------------------- 1 | /* C program to implement the transition diagram for integer */ 2 | 3 | #include 4 | #include 5 | 6 | /* Use a DFA approach. state indicates the state of the DFA. */ 7 | enum Statetype {NORMAL, FSIGN, INT, FINT}; 8 | 9 | enum Statetype start(int); 10 | enum Statetype state1(int); 11 | enum Statetype state2(int); 12 | 13 | 14 | /*------------------------------------------------------------*/ 15 | /* main: Read text from stdin. Check for the integer and */ 16 | /* and return the result to stdout. Return 0 */ 17 | /*------------------------------------------------------------*/ 18 | int main() 19 | { 20 | int c; 21 | enum Statetype state = NORMAL; 22 | for( ; ; ) { 23 | c = getchar(); 24 | if (c == EOF) 25 | break; 26 | switch (state) 27 | { 28 | case NORMAL: 29 | state = start(c); 30 | break; 31 | case FSIGN: 32 | state = state1(c); 33 | break; 34 | case INT: 35 | state = state2(c); 36 | break; 37 | case FINT: 38 | printf("Integer detected.\n"); 39 | return 0; 40 | } 41 | } 42 | return 0; 43 | } 44 | 45 | /*------------------------------------------------------------*/ 46 | /* start: Implement the state 0 of the DFA. c is the current */ 47 | /* DFA character. Return the next state. */ 48 | /*------------------------------------------------------------*/ 49 | enum Statetype start(int c) 50 | { 51 | enum Statetype state; 52 | if (c == '+' || c == '-') 53 | state = FSIGN; 54 | else if(isdigit(c)) 55 | state = INT; 56 | return state; 57 | } 58 | 59 | /*------------------------------------------------------------*/ 60 | /* state1: Implement the state 1 of the DFA. c is the current */ 61 | /* DFA character. Return the next state. */ 62 | /*------------------------------------------------------------*/ 63 | enum Statetype state1(int c) 64 | { 65 | enum Statetype state; 66 | if (isdigit(c)) 67 | state = INT; 68 | return state; 69 | } 70 | 71 | /*------------------------------------------------------------*/ 72 | /* state2: Implement the state 2 of the DFA. c is the current */ 73 | /* DFA character. Return the next state. */ 74 | /*------------------------------------------------------------*/ 75 | enum Statetype state2(int c) 76 | { 77 | enum Statetype state; 78 | if (isdigit(c)) 79 | state = INT; 80 | else 81 | state = FINT; 82 | return state; 83 | } -------------------------------------------------------------------------------- /src/c/11_c.c: -------------------------------------------------------------------------------- 1 | /* C program to implement the transition diagram for real number */ 2 | 3 | #include 4 | #include 5 | 6 | /* Use a DFA approach. state indicates the state of the DFA. */ 7 | enum Statetype {NORMAL, FSIGN, INT, DOT, REAL, SCI, SSIGN, SNOT, FINT, FREAL, FSCI}; 8 | 9 | enum Statetype start(int); 10 | enum Statetype state1(int); 11 | enum Statetype state2(int); 12 | enum Statetype state3(int); 13 | enum Statetype state4(int); 14 | enum Statetype state5(int); 15 | enum Statetype state6(int); 16 | enum Statetype state7(int); 17 | 18 | 19 | /*------------------------------------------------------------*/ 20 | /* main: Read text from stdin. Check for the floating point */ 21 | /* number and return the result to stdout. Return 0 */ 22 | /*------------------------------------------------------------*/ 23 | int main() 24 | { 25 | int c; 26 | enum Statetype state = NORMAL; 27 | for( ; ; ) { 28 | c = getchar(); 29 | if (c == EOF) 30 | break; 31 | switch (state) 32 | { 33 | case NORMAL: 34 | state = start(c); 35 | break; 36 | case FSIGN: 37 | state = state1(c); 38 | break; 39 | case INT: 40 | state = state2(c); 41 | break; 42 | case DOT: 43 | state = state3(c); 44 | break; 45 | case REAL: 46 | state = state4(c); 47 | break; 48 | case SCI: 49 | state = state5(c); 50 | break; 51 | case SSIGN: 52 | state = state6(c); 53 | break; 54 | case SNOT: 55 | state = state7(c); 56 | break; 57 | case FINT: 58 | printf("Integer detected.\n"); 59 | return 0; 60 | case FREAL: 61 | printf("Real number detected.\n"); 62 | return 0; 63 | case FSCI: 64 | printf("Scientific Notation detected.\n"); 65 | return 0; 66 | } 67 | } 68 | return 0; 69 | } 70 | 71 | /*------------------------------------------------------------*/ 72 | /* start: Implement the state 0 of the DFA. c is the current */ 73 | /* DFA character. Return the next state. */ 74 | /*------------------------------------------------------------*/ 75 | enum Statetype start(int c) 76 | { 77 | enum Statetype state; 78 | if (c == '+' || c == '-') 79 | state = FSIGN; 80 | else if(isdigit(c)) 81 | state = INT; 82 | return state; 83 | } 84 | 85 | /*------------------------------------------------------------*/ 86 | /* state1: Implement the state 1 of the DFA. c is the current */ 87 | /* DFA character. Return the next state. */ 88 | /*------------------------------------------------------------*/ 89 | enum Statetype state1(int c) 90 | { 91 | enum Statetype state; 92 | if (isdigit(c)) 93 | state = INT; 94 | return state; 95 | } 96 | 97 | /*------------------------------------------------------------*/ 98 | /* state2: Implement the state 2 of the DFA. c is the current */ 99 | /* DFA character. Return the next state. */ 100 | /*------------------------------------------------------------*/ 101 | enum Statetype state2(int c) 102 | { 103 | enum Statetype state; 104 | if (isdigit(c)) 105 | state = INT; 106 | else if (c == '.') 107 | state = DOT; 108 | else if (c == 'e' || c == 'E') 109 | state = SCI; 110 | else 111 | state = FINT; 112 | return state; 113 | } 114 | 115 | /*------------------------------------------------------------*/ 116 | /* state3: Implement the state 3 of the DFA. c is the current */ 117 | /* DFA character. Return the next state. */ 118 | /*------------------------------------------------------------*/ 119 | enum Statetype state3(int c) 120 | { 121 | enum Statetype state; 122 | if (isdigit(c)) 123 | state = REAL; 124 | return state; 125 | } 126 | 127 | /*------------------------------------------------------------*/ 128 | /* state4: Implement the state 4 of the DFA. c is the current */ 129 | /* DFA character. Return the next state. */ 130 | /*------------------------------------------------------------*/ 131 | enum Statetype state4(int c) 132 | { 133 | enum Statetype state; 134 | if (isdigit(c)) 135 | state = REAL; 136 | else if (c == 'e' || c == 'E') 137 | state = SCI; 138 | else 139 | state = FREAL; 140 | return state; 141 | } 142 | 143 | /*------------------------------------------------------------*/ 144 | /* state5: Implement the state 5 of the DFA. c is the current */ 145 | /* DFA character. Return the next state. */ 146 | /*------------------------------------------------------------*/ 147 | enum Statetype state5(int c) 148 | { 149 | enum Statetype state; 150 | if (isdigit(c)) 151 | state = SNOT; 152 | else if (c == '+' || c == '-') 153 | state = SSIGN; 154 | return state; 155 | } 156 | 157 | /*------------------------------------------------------------*/ 158 | /* state6: Implement the state 6 of the DFA. c is the current */ 159 | /* DFA character. Return the next state. */ 160 | /*------------------------------------------------------------*/ 161 | enum Statetype state6(int c) 162 | { 163 | enum Statetype state; 164 | if (isdigit(c)) 165 | state = SNOT; 166 | return state; 167 | } 168 | 169 | /*------------------------------------------------------------*/ 170 | /* state7: Implement the state 7 of the DFA. c is the current */ 171 | /* DFA character. Return the next state. */ 172 | /*------------------------------------------------------------*/ 173 | enum Statetype state7(int c) 174 | { 175 | enum Statetype state; 176 | if (isdigit(c)) 177 | state = SNOT; 178 | else 179 | state = FSCI; 180 | return state; 181 | } -------------------------------------------------------------------------------- /src/c/11_d.c: -------------------------------------------------------------------------------- 1 | /* C program to implement the transition diagram for identifier */ 2 | 3 | #include 4 | #include 5 | 6 | /* Use a DFA approach. state indicates the state of the DFA. */ 7 | enum Statetype {NORMAL, INWORD, END}; 8 | 9 | enum Statetype start(int); 10 | enum Statetype state1(int); 11 | 12 | 13 | /*------------------------------------------------------------*/ 14 | /* main: Read text from stdin. Check for the identifier and */ 15 | /* return the result to stdout. Return 0 */ 16 | /*------------------------------------------------------------*/ 17 | int main() 18 | { 19 | int c; 20 | enum Statetype state = NORMAL; 21 | for( ; ; ) { 22 | c = getchar(); 23 | if (c == EOF) 24 | break; 25 | switch (state) 26 | { 27 | case NORMAL: 28 | state = start(c); 29 | break; 30 | case INWORD: 31 | state = state1(c); 32 | break; 33 | case END: 34 | printf("Identifier detected.\n"); 35 | return 0; 36 | } 37 | } 38 | return 0; 39 | } 40 | 41 | /*------------------------------------------------------------*/ 42 | /* start: Implement the state 0 of the DFA. c is the current */ 43 | /* DFA character. Return the next state. */ 44 | /*------------------------------------------------------------*/ 45 | enum Statetype start(int c) 46 | { 47 | enum Statetype state; 48 | if (isalpha(c)) 49 | state = INWORD; 50 | return state; 51 | } 52 | 53 | /*------------------------------------------------------------*/ 54 | /* state1: Implement the state 1 of the DFA. c is the current */ 55 | /* DFA character. Return the next state. */ 56 | /*------------------------------------------------------------*/ 57 | enum Statetype state1(int c) 58 | { 59 | enum Statetype state; 60 | if (isalnum(c)) 61 | state = INWORD; 62 | else 63 | state = END; 64 | return state; 65 | } -------------------------------------------------------------------------------- /src/c/11_e.c: -------------------------------------------------------------------------------- 1 | /* C program to implement the transition diagram for relational operators */ 2 | 3 | #include 4 | 5 | /* Use a DFA approach. state indicates the state of the DFA. */ 6 | int state = 0; 7 | 8 | int start(int); 9 | int state1(int); 10 | int state6(int); 11 | 12 | 13 | /*------------------------------------------------------------*/ 14 | /* main: Read text from stdin. Check for the relop and return */ 15 | /* the result to stdout. Return 0 */ 16 | /*------------------------------------------------------------*/ 17 | int main() 18 | { 19 | int c; 20 | for( ; ; ) { 21 | c = getchar(); 22 | if (c == EOF) 23 | break; 24 | switch (state) 25 | { 26 | case 0: 27 | state = start(c); 28 | break; 29 | case 1: 30 | state = state1(c); 31 | break; 32 | case 2: 33 | printf("RELOP, LE\n"); 34 | return 0; 35 | case 3: 36 | printf("RELOP, NE\n"); 37 | return 0; 38 | case 4: 39 | printf("RELOP, LT\n"); 40 | return 0; 41 | case 5: 42 | printf("RELOP, EQ\n"); 43 | return 0; 44 | case 6: 45 | state = state6(c); 46 | break; 47 | case 7: 48 | printf("RELOP, GE\n"); 49 | return 0; 50 | case 8: 51 | printf("RELOP, GT\n"); 52 | return 0; 53 | } 54 | } 55 | return 0; 56 | } 57 | 58 | /*------------------------------------------------------------*/ 59 | /* start: Implement the state 0 of the DFA. c is the current */ 60 | /* DFA character. Return the next state. */ 61 | /*------------------------------------------------------------*/ 62 | int start(int c) 63 | { 64 | if (c == '<') 65 | state = 1; 66 | else if (c == '=') 67 | state = 5; 68 | else if (c == '>') 69 | state = 6; 70 | return state; 71 | } 72 | 73 | /*------------------------------------------------------------*/ 74 | /* state1: Implement the state 1 of the DFA. c is the current */ 75 | /* DFA character. Return the next state. */ 76 | /*------------------------------------------------------------*/ 77 | int state1(int c) 78 | { 79 | if (c == '=') 80 | state = 2; 81 | else if (c == '>') 82 | state = 3; 83 | else 84 | state = 4; 85 | return state; 86 | } 87 | 88 | /*------------------------------------------------------------*/ 89 | /* state6: Implement the state 6 of the DFA. c is the current */ 90 | /* DFA character. Return the next state. */ 91 | /*------------------------------------------------------------*/ 92 | int state6(int c) 93 | { 94 | if (c == '=') 95 | state = 7; 96 | else 97 | state = 8; 98 | return state; 99 | } -------------------------------------------------------------------------------- /src/lex/01.l: -------------------------------------------------------------------------------- 1 | /* Lex Program that copies a file, replacing each nonempty sequence of white space by a single blank */ 2 | /* Definations */ 3 | %{ 4 | /* standard io for io functions, string.h for string functions and stdlib for exit()*/ 5 | #include 6 | #include 7 | #include 8 | /* String variable to store string line by line*/ 9 | char string[200]; 10 | %} 11 | 12 | /* Rules */ 13 | /* Copy a line to the output file */ 14 | /* Replace sequence of white space by a single blank */ 15 | /* Copy everything else as it is to the line*/ 16 | /*Copy a line to the output file at the end*/ 17 | %% 18 | [\n] {fprintf(yyout, "%s\n", string); string[0] = '\0'; } 19 | [ ]* {fprintf(yyout, "%s", string); string[0] = '\0'; fprintf(yyout, "%s", " "); } 20 | . strcat(string, yytext); 21 | <> { fprintf(yyout, "%s", string); return 0; } 22 | %% 23 | 24 | int main() 25 | { 26 | extern FILE *yyin, *yyout; 27 | char filename[100]; 28 | printf("This Program is going copy a file, replacing each nonempty sequence of white space by a single blank!\nEnter the name of the file to copy:\t"); 29 | scanf("%s", filename); 30 | /* Opening file to read */ 31 | yyin = fopen(filename, "r"); 32 | if (yyin == NULL){ 33 | printf("Cannot open file %s\n", filename); 34 | exit(0); 35 | } 36 | printf("Enter the name of the file to open for writing:\t"); 37 | scanf("%s", filename); 38 | /* Opening file to write */ 39 | yyout = fopen(filename, "w"); 40 | if (yyout == NULL){ 41 | printf("Cannot open file %s\n", filename); 42 | exit(1); 43 | } 44 | yylex(); 45 | } 46 | 47 | int yywrap(void) 48 | { 49 | return 1; 50 | } -------------------------------------------------------------------------------- /src/lex/02.l: -------------------------------------------------------------------------------- 1 | /* Lex Program that copies a C program, replacing each instance of the keyword float with double */ 2 | /* Definations */ 3 | %{ 4 | /* standard io for io functions, string.h for string functions and stdlib for exit()*/ 5 | #include 6 | #include 7 | #include 8 | /* String variable to store string line by line*/ 9 | char string[200]; 10 | %} 11 | 12 | /* Rules */ 13 | /* Copy a line to the output file */ 14 | /* Replace sequence of white space by a single blank */ 15 | /* Copy everything else as it is to the line*/ 16 | /*Copy a line to the output file at the end*/ 17 | %% 18 | [\n] {fprintf(yyout, "%s\n", string); string[0] = '\0'; } 19 | "float" {fprintf(yyout, "%s", string); string[0] = '\0'; fprintf(yyout, "%s", "double"); } 20 | . strcat(string, yytext); 21 | <> { fprintf(yyout, "%s", string); return 0; } 22 | %% 23 | 24 | int main() 25 | { 26 | extern FILE *yyin, *yyout; 27 | char filename[100]; 28 | printf("This Program is going copy a C program, replacing each instance of the keyword float with double!\nEnter the name of the C file to copy:\t"); 29 | scanf("%s", filename); 30 | /* Opening file to read */ 31 | yyin = fopen(filename, "r"); 32 | if (yyin == NULL){ 33 | printf("Cannot open file %s\n", filename); 34 | exit(0); 35 | } 36 | printf("Enter the name of the C file to open for writing:\t"); 37 | scanf("%s", filename); 38 | /* Opening file to write */ 39 | yyout = fopen(filename, "w"); 40 | if (yyout == NULL){ 41 | printf("Cannot open file %s\n", filename); 42 | exit(1); 43 | } 44 | yylex(); 45 | } 46 | 47 | int yywrap(void) 48 | { 49 | return 1; 50 | } -------------------------------------------------------------------------------- /src/lex/03.l: -------------------------------------------------------------------------------- 1 | /* Lex program which will modify the words in the following way: */ 2 | /* If the first letter is a consonant, move it to the end of the word and then add ay. */ 3 | /* If the first letter is a vowel, just add ay to the end of the word. */ 4 | /* Definations */ 5 | consonant [b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z] 6 | vowel [aeiouAEIOU] 7 | letter [a-zA-Z] 8 | %{ 9 | /* Standard io for io functions */ 10 | #include 11 | %} 12 | 13 | /* Rules */ 14 | /* If the first letter is a vowel, just add ay to the end of the word. */ 15 | /* If the first letter is a consonant, move it to the end of the word and then add ay. */ 16 | /* Leave everything as it is */ 17 | %% 18 | {vowel}({letter})* {printf("%say", yytext);} 19 | {consonant}({letter})* {printf("%s%cay", yytext+1, yytext[0]);} 20 | . ECHO; 21 | \n ECHO; 22 | %% 23 | 24 | int main() 25 | { 26 | yylex(); 27 | return 0; 28 | } 29 | 30 | int yywrap(void) 31 | { 32 | return 1; 33 | } 34 | -------------------------------------------------------------------------------- /src/lex/04.l: -------------------------------------------------------------------------------- 1 | /* Lex program which will identify identifier from given file */ 2 | /* Definition Section */ 3 | digit [0-9] 4 | letter [A-Za-z] 5 | %{ 6 | #include 7 | #include 8 | %} 9 | 10 | 11 | /* Rule Section */ 12 | /* Check the identifier */ 13 | /* Ignore everything else */ 14 | %% 15 | {letter}({letter}|{digit}|_)* printf("Identifier is %s\n", yytext); 16 | .|\n ; 17 | %% 18 | 19 | 20 | int main() 21 | { 22 | extern FILE *yyin; 23 | char filename[100]; 24 | printf("This Program program checks if it is identifier or not!\nEnter the name of the file to read:\t"); 25 | scanf("%s", filename); 26 | /* Opening file to read */ 27 | yyin = fopen(filename, "r"); 28 | if (yyin == NULL){ 29 | printf("Cannot open file %s\n", filename); 30 | exit(0); 31 | } 32 | /*call the yylex function.*/ 33 | yylex(); 34 | return 0; 35 | } 36 | 37 | /*call the yywrap function*/ 38 | int yywrap() 39 | { 40 | return 1; 41 | } -------------------------------------------------------------------------------- /src/lex/05.l: -------------------------------------------------------------------------------- 1 | /* Lex program to count the number of vowels and consonants from an input file and write the results to a file. */ 2 | /* Definations */ 3 | consonant [b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z] 4 | vowel [aeiouAEIOU] 5 | %{ 6 | /* standard io for io functions and stdlib for exit()*/ 7 | #include 8 | #include 9 | /* Counter variables */ 10 | int vcounter, ccounter; 11 | %} 12 | 13 | /* Rules */ 14 | /* Count vowels */ 15 | /* Count consonants */ 16 | /* Ignore everything else */ 17 | %% 18 | {vowel} vcounter++; 19 | {consonant} ccounter++; 20 | . ; 21 | \n ; 22 | %% 23 | 24 | int main() 25 | { 26 | extern FILE *yyin, *yyout; 27 | char filename[100]; 28 | printf("This Program program counts the number of vowels and consonants from an input file and write the results to a file!\nEnter the name of the file to read:\t"); 29 | scanf("%s", filename); 30 | /* Opening file to read */ 31 | yyin = fopen(filename, "r"); 32 | if (yyin == NULL){ 33 | printf("Cannot open file %s\n", filename); 34 | exit(0); 35 | } 36 | printf("Enter the name of the file to open for writing:\t"); 37 | scanf("%s", filename); 38 | /* Opening file to write */ 39 | yyout = fopen(filename, "w"); 40 | if (yyout == NULL){ 41 | printf("Cannot open file %s\n", filename); 42 | exit(1); 43 | } 44 | yylex(); 45 | fprintf(yyout, "Vowels: %d\nConsonants: %d", vcounter, ccounter); 46 | return 0; 47 | } 48 | 49 | int yywrap(void) 50 | { 51 | return 1; 52 | } -------------------------------------------------------------------------------- /src/lex/06.l: -------------------------------------------------------------------------------- 1 | /* Lex program to count the number of lines in a given input source file. */ 2 | %{ 3 | /* standard io for io functions and stdlib for exit()*/ 4 | #include 5 | #include 6 | /* Counter variables */ 7 | int count; 8 | %} 9 | 10 | /* Rules */ 11 | /* Ignore everything else */ 12 | /* Count lines */ 13 | %% 14 | . ; 15 | \n count++; 16 | %% 17 | 18 | int main() 19 | { 20 | extern FILE *yyin; 21 | char filename[100]; 22 | printf("This Program program counts the number of lines present in an input file!\nEnter the name of the file to read:\t"); 23 | scanf("%s", filename); 24 | /* Opening file to read */ 25 | yyin = fopen(filename, "r"); 26 | if (yyin == NULL){ 27 | printf("Cannot open file %s\n", filename); 28 | exit(0); 29 | } 30 | yylex(); 31 | printf("Number of Lines %d\n", count+1); 32 | return 0; 33 | } 34 | 35 | int yywrap(void) 36 | { 37 | return 1; 38 | } -------------------------------------------------------------------------------- /src/lex/07.l: -------------------------------------------------------------------------------- 1 | /* Write a Lex program to count the number of comment lines in a c program. Also eliminate that comment line (Input read from file). */ 2 | /* Definition Section */ 3 | %{ 4 | #include 5 | #include 6 | int counter; 7 | %} 8 | 9 | 10 | /* Rule Section */ 11 | /* Check the identifier */ 12 | /* Ignore everything else */ 13 | %% 14 | [/][*][^*]*[*]+([^*/][^*]*[*]+)*[/] counter ++; 15 | .|\n ECHO; 16 | %% 17 | 18 | 19 | int main() 20 | { 21 | extern FILE *yyin; 22 | char filename[100]; 23 | printf("This Program program counts the number of comment!!\nEnter the name of the file to read:\t"); 24 | scanf("%s", filename); 25 | /* Opening file to read */ 26 | yyin = fopen(filename, "r"); 27 | if (yyin == NULL){ 28 | printf("Cannot open file %s\n", filename); 29 | exit(0); 30 | } 31 | /*call the yylex function.*/ 32 | yylex(); 33 | printf("\nNumber of comments: %d\n", counter); 34 | return 0; 35 | } 36 | 37 | /*call the yywrap function*/ 38 | int yywrap() 39 | { 40 | return 1; 41 | } -------------------------------------------------------------------------------- /src/lex/08.l: -------------------------------------------------------------------------------- 1 | /* Lex program which will identify 8-bit binary input file */ 2 | /* Definition Section */ 3 | bit [0-1] 4 | %{ 5 | #include 6 | #include 7 | %} 8 | 9 | 10 | /* Rule Section */ 11 | /* Check the input */ 12 | /* Ignore everything else */ 13 | %% 14 | {bit}{bit}{bit}{bit}{bit}{bit}{bit}{bit} printf("%s is a 8-bit Binary number\n", yytext); 15 | {bit}+ printf("%s is not a 8-bit Binary number\n", yytext); 16 | .|\n ; 17 | %% 18 | int main() 19 | { 20 | char input[100]; 21 | printf("This Program checks input is 8-bit binary number!\nEnter the binary number\t"); 22 | /*call the yylex function.*/ 23 | yylex(); 24 | return 0; 25 | } 26 | 27 | /*call the yywrap function*/ 28 | int yywrap() 29 | { 30 | return 1; 31 | } 32 | -------------------------------------------------------------------------------- /src/lex/09.l: -------------------------------------------------------------------------------- 1 | /* lex program to find out all the tokens input C program. */ 2 | /* Definition Section */ 3 | digit [0-9] 4 | letter [a-zA-Z] 5 | %{ 6 | #include 7 | #include 8 | %} 9 | 10 | 11 | /* Rule Section */ 12 | /* Token specification */ 13 | %% 14 | "int" printf("Token: INT\n"); 15 | "(" printf("Token: (\n"); 16 | ")" printf("Token: )\n"); 17 | "{" printf("Token: {\n"); 18 | "}" printf("Token: }\n"); 19 | ";" printf("Token: End of Statement\n"); 20 | "//".* ; 21 | "return" printf("Token: RETURN\n"); 22 | "," printf("Token: , operator\n"); 23 | "=" printf("Token: assignment operator\n"); 24 | {letter}({letter}|{digit})* printf("Token: identifier\n"); 25 | {digit}+ printf("Token: Integer value\n"); 26 | .|\n ; 27 | %% 28 | int main() 29 | { 30 | extern FILE *yyin; 31 | char filename[100]; 32 | printf("This Program program finds all tokens!!\nEnter the name of the file to read:\t"); 33 | scanf("%s", filename); 34 | /* Opening file to read */ 35 | yyin = fopen(filename, "r"); 36 | if (yyin == NULL){ 37 | printf("Cannot open file %s\n", filename); 38 | exit(0); 39 | } 40 | /*call the yylex function.*/ 41 | yylex(); 42 | return 0; 43 | } 44 | 45 | /*call the yywrap function*/ 46 | int yywrap() 47 | { 48 | return 1; 49 | } -------------------------------------------------------------------------------- /src/lex/10.l: -------------------------------------------------------------------------------- 1 | /* Lex program to count the number of tokens and find out all the tokens from input C program. */ 2 | /* Definition Section */ 3 | digit [0-9] 4 | letter [a-zA-Z] 5 | %{ 6 | #include 7 | #include 8 | int counter = 0; 9 | %} 10 | 11 | 12 | /* Rule Section */ 13 | /* Token specification */ 14 | %% 15 | \"(\\.|[^"\\])*\" {printf("Token: string\n"); counter++;} 16 | "int" {printf("Token: INT\n"); counter++;} 17 | "(" {printf("Token: (\n"); counter++;} 18 | ")" {printf("Token: )\n"); counter++;} 19 | "{" {printf("Token: {\n"); counter++;} 20 | "}" {printf("Token: }\n"); counter++;} 21 | ";" {printf("Token: End of Statement\n"); counter++;} 22 | "//".* ; 23 | "return" {printf("Token: RETURN\n"); counter++;} 24 | "," {printf("Token: , operator\n"); counter++;} 25 | "=" {printf("Token: assignment operator\n"); counter++;} 26 | "+" {printf("Token: addition operator\n"); counter++;} 27 | {letter}({letter}|{digit})* {printf("Token: identifier\n"); counter++;} 28 | {digit}+ {printf("Token: Integer value\n"); counter++;} 29 | .|\n ; 30 | %% 31 | int main() 32 | { 33 | extern FILE *yyin; 34 | char filename[100]; 35 | printf("This Program program finds all tokens!!\nEnter the name of the file to read:\t"); 36 | scanf("%s", filename); 37 | /* Opening file to read */ 38 | yyin = fopen(filename, "r"); 39 | if (yyin == NULL){ 40 | printf("Cannot open file %s\n", filename); 41 | exit(0); 42 | } 43 | /*call the yylex function.*/ 44 | yylex(); 45 | printf("Token count: %d\n", counter); 46 | return 0; 47 | } 48 | 49 | /*call the yywrap function*/ 50 | int yywrap() 51 | { 52 | return 1; 53 | } -------------------------------------------------------------------------------- /src/lex/12.l: -------------------------------------------------------------------------------- 1 | /* Lex program to identify all the numbers in an input text and then perform the sum of the numbers. */ 2 | /* Definition Section */ 3 | digits [0-9]+|([0-9]*)"."([0-9]+) 4 | %{ 5 | #include 6 | #include 7 | float sum = 0, a; 8 | %} 9 | 10 | 11 | /* Rule Section */ 12 | %% 13 | {digits} {printf("Number: %s\n", yytext); sum += atof(yytext);} 14 | . ; 15 | \n {printf("Sum : %f\n", sum); sum = 0;} 16 | %% 17 | 18 | int main() 19 | { 20 | yylex(); 21 | return 0; 22 | } 23 | 24 | /*call the yywrap function*/ 25 | int yywrap() 26 | { 27 | return 1; 28 | } -------------------------------------------------------------------------------- /src/lex/13.l: -------------------------------------------------------------------------------- 1 | /* Lex program to count the number "The" in a input text */ 2 | /* Definations */ 3 | the ("the"|"The"|"THE") 4 | %{ 5 | /* standard io for io functions and stdlib for exit()*/ 6 | #include 7 | #include 8 | /* Counter variables */ 9 | int count; 10 | %} 11 | 12 | /* Rules */ 13 | /* Count "the" */ 14 | /* Ignore everything else */ 15 | %% 16 | {the} count++; 17 | .|\n ; 18 | 19 | %% 20 | 21 | int main() 22 | { 23 | char input[500]; 24 | printf("This Program counts the number of \"the\" present in the input text\nEnter the text\t"); 25 | /*call the yylex function.*/ 26 | yylex(); 27 | printf("\nNumber of the: %d\n", count); 28 | return 0; 29 | } 30 | 31 | /*call the yywrap function*/ 32 | int yywrap() 33 | { 34 | return 1; 35 | } -------------------------------------------------------------------------------- /src/lex/14.l: -------------------------------------------------------------------------------- 1 | /* lex program to count the number of stopwords and remove all of them. */ 2 | /* Definition Section */ 3 | stopwords (" ourselves "|" hers "|" between "|" yourself "|" but "|" again "|" there "|" about "|" once "|" during "|" out "|" very "|" having "|" with "|" they "|" own "|" an "|" be "|" some "|" for "|" do "|" its "|" yours "|" such "|" into "|" of "|" most "|" itself "|" other "|" off "|" is "|" s "|" am "|" or "|" who "|" as "|" from "|" him "|" each "|" the "|" themselves "|" until "|" below "|" are "|" we "|" these "|" your "|" his "|" through "|" don "|" nor "|" me "|" were "|" her "|" more "|" himself "|" this "|" down "|" should "|" our "|" their "|" while "|" above "|" both "|" up "|" to "|" ours "|" had "|" she "|" all "|" no "|" when "|" at "|" any "|" before "|" them "|" same "|" and "|" been "|" have "|" in "|" will "|" on "|" does "|" yourselves "|" then "|" that "|" because "|" what "|" over "|" why "|" so "|" can "|" did "|" not "|" now "|" under "|" he "|" you "|" herself "|" has "|" just "|" where "|" too "|" only "|" myself "|" which "|" those "|" i "|" after "|" few "|" whom "|" t "|" being "|" if "|" theirs "|" my "|" against "|" a "|" by "|" doing "|" it "|" how "|" further "|" was "|" here "|" than ") 4 | %{ 5 | #include 6 | #include 7 | int counter; 8 | %} 9 | 10 | /* Rule Section */ 11 | /* Check the stopwords */ 12 | /* Ignore everything else */ 13 | 14 | %% 15 | {stopwords} {counter++;} 16 | .|\n ECHO; 17 | %% 18 | 19 | int main() 20 | { 21 | extern FILE *yyin; 22 | char filename[100]; 23 | printf("This Program program counts the number of stopwords!!\nEnter the name of the file to read:\t"); 24 | scanf("%s", filename); 25 | /* Opening file to read */ 26 | yyin = fopen(filename, "r"); 27 | if (yyin == NULL){ 28 | printf("Cannot open file %s\n", filename); 29 | exit(0); 30 | } 31 | /*call the yylex function.*/ 32 | yylex(); 33 | printf("\nNumber of stopwords: %d\n", counter); 34 | return 0; 35 | } 36 | 37 | /*call the yywrap function*/ 38 | int yywrap() 39 | { 40 | return 1; 41 | } 42 | -------------------------------------------------------------------------------- /src/lex/16.l: -------------------------------------------------------------------------------- 1 | /* Lex program to implement a simple calculator. */ 2 | /* Definition Section */ 3 | %{ 4 | /* Standard io for io functions, string.h for string functions and stdlib for exit()*/ 5 | #include 6 | #include 7 | #include 8 | /* String variable to store string line by line*/ 9 | int op = 0, i; 10 | float a, b; 11 | void calculate(); 12 | %} 13 | digits [0-9]+|([0-9]*)"."([0-9]+) 14 | addition "+" 15 | subtraction "-" 16 | multiplication "*" 17 | division "/" 18 | power "^" 19 | endofline \n 20 | 21 | /* Rule Section */ 22 | %% 23 | {digits} {calculate();} 24 | {addition} {op=1;} 25 | {subtraction} {op=2;} 26 | {multiplication} {op=3;} 27 | {division} {op=4;} 28 | {power} {op=5;} 29 | {endofline} {printf("Answer :%f\n\n",a);} 30 | 31 | 32 | %% 33 | void calculate() 34 | { 35 | if (op == 0) 36 | 37 | /* atof() is used to convert - the ASCII input to float */ 38 | a = atof(yytext); 39 | 40 | else 41 | { 42 | b = atof(yytext); 43 | 44 | switch (op) 45 | { 46 | case 1: 47 | a = a + b; 48 | break; 49 | 50 | case 2: 51 | a = a - b; 52 | break; 53 | 54 | case 3: 55 | a = a * b; 56 | break; 57 | 58 | case 4: 59 | a = a / b; 60 | break; 61 | 62 | case 5: 63 | for (i = a; b > 1; b--) 64 | a = a * i; 65 | break; 66 | } 67 | op = 0; 68 | } 69 | } 70 | 71 | int main() 72 | { 73 | yylex(); 74 | return 0; 75 | } 76 | 77 | int yywrap() 78 | { 79 | return 1; 80 | } 81 | -------------------------------------------------------------------------------- /src/lex/17.l: -------------------------------------------------------------------------------- 1 | /* Lex program to count the number of palindromes present in a input text */ 2 | /* and write them to a separate text file */ 3 | %{ 4 | #include 5 | #include 6 | int i, len, counter; 7 | %} 8 | 9 | /* Rule Section */ 10 | /* Count palindrome */ 11 | /* Ignore everything else */ 12 | %% 13 | [a-zA-Z0-9]+ { 14 | len=strlen(yytext); 15 | for(i=0;i 6 | int v = 0, op = 0, id = 0, flag = 0; 7 | %} 8 | /* Rules */ 9 | %% 10 | 11 | [0-9 a-z A-z]+ {id++;printf("\n Identifier:\t");ECHO;} 12 | [+ | \-\ | / | = | *] {op++; printf("\n Operator:\t");ECHO;} 13 | "(" {v++;} 14 | ")" {v--;} 15 | ";" {flag = 1;} 16 | .|\n {;} 17 | %% 18 | 19 | int main() 20 | { 21 | printf("Enter the expression \n"); 22 | yylex(); 23 | if((op+1) == id && v==0 && flag==0){ 24 | printf("\nThe given Expression is valid\n"); 25 | printf("\nThe no. of Identifiers are %d\n",id); 26 | printf("\nThe no. of operators are %d\n",op); 27 | } 28 | else{ 29 | printf("\nThe given Expresion is Invalid\n"); 30 | } 31 | return 0; 32 | } 33 | int yywrap() 34 | { 35 | return 1; 36 | } -------------------------------------------------------------------------------- /src/lex/19.l: -------------------------------------------------------------------------------- 1 | /*lex program to find the length of a string*/ 2 | %{ 3 | #include 4 | int length; 5 | %} 6 | /* Rule Section */ 7 | /* Find length of a string */ 8 | /* Ignore everything else */ 9 | %% 10 | .+ {length=yyleng; } 11 | \n ; 12 | %% 13 | 14 | int main() 15 | { 16 | printf("This Program checks the length in an input text!\nEnter the input text\t"); 17 | /*call the yylex function.*/ 18 | yylex(); 19 | printf("length of input text is : %d\n", length); 20 | return 0; 21 | } 22 | 23 | /*call the yywrap function*/ 24 | int yywrap() 25 | { 26 | return 1; 27 | } 28 | -------------------------------------------------------------------------------- /src/lex/20.l: -------------------------------------------------------------------------------- 1 | /* Lex program to reverse all the words in an input text. */ 2 | /* Definations */ 3 | %{ 4 | /* standard io for io functions and strigs.h for strings*/ 5 | #include 6 | #include 7 | %} 8 | 9 | /* Rules */ 10 | 11 | %% 12 | [a-zA-Z0-9]+ { 13 | for(int i = yyleng - 1; i >= 0; i--) { 14 | printf("%c", yytext[i]); 15 | } 16 | } 17 | .|\n ECHO; 18 | 19 | %% 20 | 21 | int main() 22 | { 23 | printf("This Program reverses all the words in an input text.\n"); 24 | /*call the yylex function.*/ 25 | yylex(); 26 | return 0; 27 | } 28 | 29 | /*call the yywrap function*/ 30 | int yywrap() 31 | { 32 | return 1; 33 | } -------------------------------------------------------------------------------- /src/lex/21.l: -------------------------------------------------------------------------------- 1 | /* Lex program to find the smallest word in an input text */ 2 | /* Definations */ 3 | %{ 4 | /* standard io for io functions and strigs.h for strings*/ 5 | #include 6 | #include 7 | /* Counter variables */ 8 | int small=9999, len; 9 | char string[200], word[200]; 10 | %} 11 | 12 | /* Rules */ 13 | /* Count small */ 14 | /* Ignore everything else */ 15 | %% 16 | [ \t\n]* { 17 | len = strlen(string); 18 | if(small > len){ 19 | small = len; 20 | strcpy(word, string); 21 | } 22 | string[0] = '\0'; 23 | } 24 | . strcat(string, yytext); 25 | 26 | %% 27 | 28 | int main() 29 | { 30 | printf("This Program finds the smallest word in the input text\nEnter the text\t"); 31 | /*call the yylex function.*/ 32 | yylex(); 33 | printf("\nThe smallest word is %s\n", word); 34 | return 0; 35 | } 36 | 37 | /*call the yywrap function*/ 38 | int yywrap() 39 | { 40 | return 1; 41 | } -------------------------------------------------------------------------------- /src/lex/22.l: -------------------------------------------------------------------------------- 1 | /* Lex Program to Convert Lowercase Characters to Uppercase. */ 2 | /* Definations */ 3 | lower [a-z] 4 | %{ 5 | /* standard io for io functions */ 6 | #include 7 | %} 8 | 9 | /* Rules */ 10 | /* Convert Lowercase to uppercase */ 11 | /* Ignore everything else */ 12 | %% 13 | {lower} {printf("%c",yytext[0]-32);} 14 | \n ECHO; 15 | . ECHO; 16 | %% 17 | 18 | int main() 19 | { 20 | printf("This Program convert Lowercase Characters to Uppercase\nEnter the input text\t"); 21 | yylex(); 22 | return 0; 23 | } 24 | 25 | /*call the yywrap function*/ 26 | int yywrap() 27 | { 28 | return 1; 29 | } -------------------------------------------------------------------------------- /src/lex/23.l: -------------------------------------------------------------------------------- 1 | /* Lex program to sort all the words in an input text as per their length. */ 2 | /* Definations */ 3 | %{ 4 | /* standard io for io functions and strigs.h for strings*/ 5 | #include 6 | #include 7 | #include 8 | 9 | int compare(const void *, const void *); 10 | char str[20][40]; 11 | int i = 0, n; 12 | %} 13 | 14 | /* Rules */ 15 | 16 | %% 17 | [a-zA-Z0-9]* { 18 | strcpy(str[i], yytext); 19 | i++; 20 | } 21 | .|\n ; 22 | 23 | %% 24 | 25 | int main() 26 | { 27 | printf("This Program sorts all the words in an input text as per their length.\n"); 28 | printf("\nPlease enter number of words :\t"); 29 | scanf("%d", &n); 30 | /*call the yylex function.*/ 31 | yylex(); 32 | qsort(str, (n - 1), 40, compare); 33 | for(int i = 0; i < n; i++) { 34 | printf("%s ", str[i]); 35 | } 36 | return 0; 37 | } 38 | 39 | 40 | int compare (const void * a, const void * b) { 41 | size_t fa = strlen((const char *)a); 42 | size_t fb = strlen((const char *)b); 43 | return (fa > fb) - (fa < fb); 44 | } 45 | 46 | /*call the yywrap function*/ 47 | int yywrap() 48 | { 49 | return 1; 50 | } -------------------------------------------------------------------------------- /src/yacc/binaryToDecimal/binToDec.l: -------------------------------------------------------------------------------- 1 | /* lex program for conversion of binary to decimal */ 2 | %{ 3 | /* generated by the yacc program */ 4 | #include 5 | #include 6 | #include "y.tab.h" 7 | %} 8 | 9 | /* Rules for syntax analyser */ 10 | %% 11 | exit.* {return exit_command;} 12 | quit.* {return exit_command;} 13 | [01] {yylval.val = atoi(yytext); return binary;} 14 | \n {return newline;} 15 | [ \t] ; 16 | . {ECHO; yyerror("Unexpected Character");} 17 | %% 18 | int yywrap() { 19 | return 1; 20 | } -------------------------------------------------------------------------------- /src/yacc/binaryToDecimal/binToDec.y: -------------------------------------------------------------------------------- 1 | /* yacc program for binary to decimal conversion */ 2 | %{ 3 | void yyerror(char *); 4 | #include 5 | #include 6 | %} 7 | 8 | /* yacc declaration */ 9 | %union {int val;} 10 | %start lines 11 | %token binary 12 | %token newline 13 | %token exit_command 14 | %type left lines 15 | 16 | /* Grammar rules with actions */ 17 | %% 18 | lines: 19 | | lines left newline {$$ = $2; printf("The Decimal Value is %d.\n>> ", $$);} 20 | | lines exit_command {exit(EXIT_SUCCESS);} 21 | ; 22 | left: left binary {$$ = $1 * 2 + $2;} 23 | | binary {$$ = $1;} 24 | ; 25 | %% 26 | /* main function */ 27 | int main() { 28 | printf("Conversion of binary to decimal built using YACC and LEX\n"); 29 | printf("Reserved keywords: exit, quit\n"); 30 | printf(">> "); 31 | return yyparse(); 32 | } 33 | 34 | void yyerror(char *s) { 35 | fprintf(stderr, "%s\n", s); 36 | } -------------------------------------------------------------------------------- /src/yacc/binaryToDecimal/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickydebojeet/compiler-programs/20bf7ae2db4f3d064e00a414a1159a7a99e4ec39/src/yacc/binaryToDecimal/output.png -------------------------------------------------------------------------------- /src/yacc/calculator/calc.l: -------------------------------------------------------------------------------- 1 | /* lex program for simple calculator */ 2 | %{ 3 | #include "y.tab.h" 4 | %} 5 | 6 | /* Lex definations */ 7 | %% 8 | "print" {return print;} 9 | "exit" {return exit_command;} 10 | [a-zA-Z] {yylval.id = yytext[0]; return identifier;} 11 | [0-9]+ {yylval.num = atoi(yytext); return number;} 12 | [ \t\n] ; 13 | [-+=*/;] {return yytext[0];} 14 | . {ECHO; yyerror("Unexpected Character");} 15 | %% 16 | int yywrap() { 17 | return 1; 18 | } -------------------------------------------------------------------------------- /src/yacc/calculator/calc.y: -------------------------------------------------------------------------------- 1 | /* yacc program for simple calculator */ 2 | %{ 3 | void yyerror(char *); 4 | #include 5 | #include 6 | int symbols[52]; 7 | int symbolVal(char); 8 | void updateSymbolVal(char, int); 9 | %} 10 | 11 | /* yacc definations */ 12 | %union {int num; char id;} 13 | %start line 14 | %token print 15 | %token exit_command 16 | %token number 17 | %token identifier 18 | %type line expe expt expf 19 | %type assignment 20 | 21 | /* Grammar definations with actions */ 22 | %% 23 | line: assignment ';' {;} 24 | | exit_command ';' {exit(EXIT_SUCCESS);} 25 | | print expe ';' {printf("Printing %d\n", $2);} 26 | | line assignment ';' {;} 27 | | line print expe ';' {printf("Printing %d\n", $3);} 28 | | line exit_command ';' {exit(EXIT_SUCCESS);} 29 | ; 30 | assignment: identifier '=' expe {updateSymbolVal($1, $3);} 31 | ; 32 | expe: expe '+' expt {$$ = $1 + $3;} 33 | | expe '-' expt {$$ = $1 - $3;} 34 | | expt {$$ = $1;} 35 | ; 36 | expt: expt '*' expf {$$ = $1 * $3;} 37 | | expt '/' expf {$$ = $1 / $3;} 38 | | expf {$$ = $1;} 39 | ; 40 | expf: '(' expe ')' {$$ = $2;} 41 | | number {$$ = $1;} 42 | | identifier {$$ = symbolVal($1);} 43 | ; 44 | %% 45 | 46 | /* This function computes the index value for a given character */ 47 | int computeSymbolIndex(char token) { 48 | int idx = -1; 49 | if(islower(token)) { 50 | idx = token - 'a' + 26; 51 | } 52 | else if(isupper(token)) { 53 | idx = token -'A'; 54 | } 55 | return idx; 56 | } 57 | 58 | /* This function looks up the value of an identifier */ 59 | int symbolVal(char symbol) { 60 | int bucket = computeSymbolIndex(symbol); 61 | return symbols[bucket]; 62 | } 63 | 64 | /* This function updates the value of an identifier */ 65 | void updateSymbolVal(char symbol, int val) { 66 | int bucket = computeSymbolIndex(symbol); 67 | symbols[bucket] = val; 68 | } 69 | 70 | /* main function */ 71 | int main() { 72 | /* Initialising all values of identifier as 0 */ 73 | int i; 74 | for(i = 0; i < 52; i++) { 75 | symbols[i] = 0; 76 | } 77 | printf("Calculator built using YACC and LEX\n"); 78 | printf("Reserved keywords: exit, print\n"); 79 | return yyparse(); 80 | } 81 | 82 | void yyerror(char *s) { 83 | fprintf(stderr, "%s\n", s); 84 | } -------------------------------------------------------------------------------- /src/yacc/calculator/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickydebojeet/compiler-programs/20bf7ae2db4f3d064e00a414a1159a7a99e4ec39/src/yacc/calculator/output.png -------------------------------------------------------------------------------- /src/yacc/contextFreeGrammar/cfg.l: -------------------------------------------------------------------------------- 1 | /* lex program for recognition of contxt free grammar */ 2 | %{ 3 | #include 4 | #include 5 | /* Generated by yacc */ 6 | #include "y.tab.h" 7 | %} 8 | %% 9 | exit.* {return exit_command;} 10 | quit.* {return exit_command;} 11 | [ba] {return yytext[0];} 12 | \n {return newline;} 13 | [ \t] ; 14 | . {ECHO; yyerror("Unexpected Character");} 15 | %% 16 | int yywrap() { 17 | return 1; 18 | } -------------------------------------------------------------------------------- /src/yacc/contextFreeGrammar/cfg.y: -------------------------------------------------------------------------------- 1 | /* yacc program for recognition of context free grammar */ 2 | %{ 3 | void yyerror(char *); 4 | #include 5 | #include 6 | %} 7 | 8 | /* yacc definations */ 9 | %start A 10 | %token newline 11 | %token exit_command 12 | 13 | /* Grammar definations with actions */ 14 | %% 15 | A: 16 | | A S newline {printf("The String belongs to the CFG.\n>> ");} 17 | | A exit_command {exit(EXIT_SUCCESS);} 18 | ; 19 | S: 'b' S 'a' {;} 20 | | 'b' 'a' {;} 21 | ; 22 | %% 23 | 24 | /* main function */ 25 | int main() { 26 | printf("Recognition of context free grammar built using YACC and LEX\n"); 27 | printf("Reserved keywords: exit, quit\n"); 28 | printf(">> "); 29 | return yyparse(); 30 | } 31 | 32 | void yyerror(char *s) { 33 | fprintf(stderr, "%s\n", s); 34 | } -------------------------------------------------------------------------------- /src/yacc/contextFreeGrammar/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickydebojeet/compiler-programs/20bf7ae2db4f3d064e00a414a1159a7a99e4ec39/src/yacc/contextFreeGrammar/output.png -------------------------------------------------------------------------------- /src/yacc/evaluation/evaluation.l: -------------------------------------------------------------------------------- 1 | /* lex program for evaluation of expression */ 2 | %{ 3 | #include 4 | #include 5 | #include 6 | /* Generated by yacc */ 7 | #include "y.tab.h" 8 | %} 9 | INTEGER [0-9]+ 10 | 11 | /* lex definations */ 12 | %% 13 | exit.* {return exit_command;} 14 | quit.* {return exit_command;} 15 | {INTEGER} {yylval.val = atoi(yytext); return number;} 16 | [+-/*()] {return yytext[0];} 17 | \n {return newline;} 18 | [ \t] ; 19 | . {ECHO; yyerror("Unexpected Character");} 20 | %% 21 | 22 | int yywrap() { 23 | return 1; 24 | } -------------------------------------------------------------------------------- /src/yacc/evaluation/evaluation.y: -------------------------------------------------------------------------------- 1 | /* yacc program for evaluation of expression */ 2 | %{ 3 | void yyerror(char *); 4 | #include 5 | #include 6 | #include 7 | %} 8 | /* yacc definations */ 9 | %union {int val;} 10 | %start lines 11 | %token number 12 | %token newline 13 | %token exit_command 14 | %type expe expt expf lines 15 | 16 | /* Grammar Definations with actions */ 17 | %% 18 | lines: 19 | | lines expe newline {printf("%d\n>> ", $2);} 20 | ; 21 | expe: expe '+' expt {$$ = $1 + $3;} 22 | | expe '-' expt {$$ = $1 - $3;} 23 | | expt {$$ = $1;} 24 | ; 25 | expt: expt '*' expf {$$ = $1 * $3;} 26 | | expt '/' expf {$$ = $1 / $3;} 27 | | expf {$$ = $1;} 28 | ; 29 | expf: '(' expe ')' {$$ = $2;} 30 | | number {$$ = $1;} 31 | | exit_command {exit(EXIT_SUCCESS);} 32 | ; 33 | %% 34 | 35 | /* main function */ 36 | int main() { 37 | printf("Evaluation of arithmetic expression built using YACC and LEX\n"); 38 | printf("Reserved keywords: exit, quit\n"); 39 | printf(">> "); 40 | return yyparse(); 41 | } 42 | 43 | void yyerror(char *s) { 44 | fprintf(stderr, "%s\n", s); 45 | exit(1); 46 | } -------------------------------------------------------------------------------- /src/yacc/evaluation/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickydebojeet/compiler-programs/20bf7ae2db4f3d064e00a414a1159a7a99e4ec39/src/yacc/evaluation/output.png -------------------------------------------------------------------------------- /src/yacc/infixToPostfix/inToPost.l: -------------------------------------------------------------------------------- 1 | /* lex program for conversion of infix to postfix */ 2 | %{ 3 | #include 4 | #include 5 | /* Generated by yacc */ 6 | #include "y.tab.h" 7 | %} 8 | 9 | /* Lex definations */ 10 | %% 11 | exit.* {return exit_command;} 12 | quit.* {return exit_command;} 13 | [0-9]+ {yylval.number = atoi(yytext); return num;} 14 | [+*-/] {return yytext[0];} 15 | \n {return newline;} 16 | [ \t] ; 17 | . {ECHO; yyerror("Unexpected Character");} 18 | %% 19 | 20 | int yywrap() { 21 | return 1; 22 | } -------------------------------------------------------------------------------- /src/yacc/infixToPostfix/inToPost.y: -------------------------------------------------------------------------------- 1 | /* yacc program for conversion of infix to postfix */ 2 | %{ 3 | void yyerror(char *); 4 | #include 5 | #include 6 | %} 7 | 8 | /* yacc definations */ 9 | %union {int number;} 10 | %start lines 11 | %token num 12 | %token newline 13 | %token exit_command 14 | 15 | /* Grammar definations with actions */ 16 | %% 17 | lines: 18 | | lines E newline {;} 19 | | lines exit_command {exit(EXIT_SUCCESS);} 20 | ; 21 | E: E '+' T {printf("+");} 22 | | E '-' T {printf("-");} 23 | | T {;} 24 | ; 25 | T: T '*' F {printf("*");} 26 | | T '/' F {printf("/");} 27 | | F {;} 28 | ; 29 | F: num {printf("%d", $1);} 30 | ; 31 | %% 32 | 33 | /* main function */ 34 | int main() { 35 | return yyparse(); 36 | return 0; 37 | } 38 | 39 | void yyerror(char *s) { 40 | fprintf(stderr, "%s\n", s); 41 | exit(1); 42 | } -------------------------------------------------------------------------------- /src/yacc/infixToPostfix/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickydebojeet/compiler-programs/20bf7ae2db4f3d064e00a414a1159a7a99e4ec39/src/yacc/infixToPostfix/output.png -------------------------------------------------------------------------------- /src/yacc/infixToPrefix/inToPre.l: -------------------------------------------------------------------------------- 1 | /* lex program for conversion of infix to prefix */ 2 | %{ 3 | #include 4 | #include 5 | #include 6 | /* Generated by yacc */ 7 | #include "y.tab.h" 8 | %} 9 | INTEGER [0-9]+ 10 | IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* 11 | 12 | /* Lex Definitions */ 13 | %% 14 | exit.* {return exit_command;} 15 | quit.* {return exit_command;} 16 | {INTEGER} {yylval.exp = strdup(yytext); return integer;} 17 | {IDENTIFIER} {yylval.exp = strdup(yytext); return identifier;} 18 | [+-] {yylval.exp = strdup(yytext); return opr1;} 19 | [/*] {yylval.exp = strdup(yytext); return opr2;} 20 | [()] {return yytext[0];} 21 | \n {return newline;} 22 | [ \t] ; 23 | . {ECHO; yyerror("Unexpected Character");} 24 | %% 25 | 26 | int yywrap() { 27 | return 1; 28 | } -------------------------------------------------------------------------------- /src/yacc/infixToPrefix/inToPre.y: -------------------------------------------------------------------------------- 1 | /* yacc program for conversion of infix to prefix */ 2 | %{ 3 | void yyerror(char *); 4 | #include 5 | #include 6 | #include 7 | char* concat( const char*, const char*, const char*); 8 | %} 9 | 10 | /* yacc definitions */ 11 | %union {char* exp;} 12 | %start lines 13 | %token integer 14 | %token identifier 15 | %token opr1 16 | %token opr2 17 | %token newline 18 | %token exit_command 19 | %left opr1 20 | %left opr2 21 | %type expe expt expf lines 22 | 23 | /* Grammar Definitions with actions */ 24 | %% 25 | lines: 26 | | lines expe newline {printf("%s\n>> ", $2);} 27 | ; 28 | expe: expe opr1 expt {$$ = concat($2, $1, $3);} 29 | | expt {$$ = $1;} 30 | ; 31 | expt: expt opr2 expf {$$ = concat($2, $1, $3);} 32 | | expf {$$ = $1;} 33 | ; 34 | expf: '(' expe ')' {$$ = $2;} 35 | | integer {$$ = concat(" ", $1, " ");} 36 | | identifier {$$ = concat(" ", $1, " ");} 37 | | exit_command {exit(EXIT_SUCCESS);} 38 | ; 39 | %% 40 | 41 | /* main function */ 42 | int main() { 43 | printf("Infix to Prefix Converter built using YACC and LEX\n"); 44 | printf("Reserved keywords: exit, quit\n"); 45 | printf(">> "); 46 | return yyparse(); 47 | } 48 | 49 | void yyerror(char *s) { 50 | fprintf(stderr, "%s\n", s); 51 | exit(1); 52 | } 53 | 54 | /* This function concatanates three string and returns it */ 55 | char* concat( const char* s1, const char* s2, const char* s3) 56 | { 57 | int len = strlen(s1) + strlen(s2) + strlen(s3) + 1; 58 | char *s = malloc(sizeof(char)*len); 59 | 60 | int i=0; 61 | for(int j=0; s1[j]!='\0'; j++) 62 | s[i++] = s1[j]; 63 | for(int j=0; s2[j]!='\0'; j++) 64 | s[i++] = s2[j]; 65 | for(int j=0; s3[j]!='\0'; j++) 66 | s[i++] = s3[j]; 67 | 68 | s[i] = '\0'; 69 | 70 | return s; 71 | } -------------------------------------------------------------------------------- /src/yacc/infixToPrefix/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickydebojeet/compiler-programs/20bf7ae2db4f3d064e00a414a1159a7a99e4ec39/src/yacc/infixToPrefix/output.png -------------------------------------------------------------------------------- /src/yacc/palindrome/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickydebojeet/compiler-programs/20bf7ae2db4f3d064e00a414a1159a7a99e4ec39/src/yacc/palindrome/output.png -------------------------------------------------------------------------------- /src/yacc/palindrome/palindrome.l: -------------------------------------------------------------------------------- 1 | /* lex program for recognition of palindrome */ 2 | %{ 3 | #include 4 | #include 5 | #include 6 | /* Generated by yacc */ 7 | #include "y.tab.h" 8 | %} 9 | 10 | /* Lex definitions */ 11 | %% 12 | exit.* {return exit_command;} 13 | quit.* {return exit_command;} 14 | [a-zA-Z]+ {yylval.string = yytext; return STR;} 15 | \n {return newline;} 16 | [ \t] ; 17 | . {ECHO; yyerror("Unexpected Character");} 18 | %% 19 | 20 | int yywrap() { 21 | return 1; 22 | } -------------------------------------------------------------------------------- /src/yacc/palindrome/palindrome.y: -------------------------------------------------------------------------------- 1 | /* yacc program for recognition of palindrome */ 2 | %{ 3 | void yyerror(char *); 4 | #include 5 | #include 6 | #include 7 | int flag, k, i; 8 | %} 9 | 10 | /* yacc definitions */ 11 | %union {char *string;} 12 | %start lines 13 | %token newline 14 | %token exit_command 15 | %token STR 16 | %type E S 17 | 18 | /* Grammar definitions with actions */ 19 | %% 20 | lines: 21 | | lines S newline {;} 22 | | lines exit_command {exit(EXIT_SUCCESS);} 23 | ; 24 | S: E { 25 | flag = 0; 26 | k = strlen($1) - 1; 27 | if(k%2==0){ 28 | for (i = 0; i <= k/2; i++) { 29 | if ($1[i] == $1[k-i]) {} 30 | else { 31 | flag = 1; 32 | } 33 | } 34 | if (flag == 1) printf("Not palindrome\n"); 35 | else printf("palindrome\n"); 36 | printf("%s\n>> ", $1); 37 | } else { 38 | for (i = 0; i < k/2; i++) { 39 | if ($1[i] == $1[k-i]) {} 40 | else { 41 | flag = 1; 42 | } 43 | } 44 | if (flag == 1) printf("Not palindrome\n"); 45 | else printf("palindrome\n"); 46 | printf("%s\n>> ", $1); 47 | } 48 | } 49 | ; 50 | E: STR {$$ = $1;} 51 | ; 52 | %% 53 | 54 | int main() { 55 | printf("Palindrome checker built using YACC and LEX\n"); 56 | printf("Reserved keywords: exit, quit\n"); 57 | printf(">> "); 58 | return yyparse(); 59 | } 60 | 61 | void yyerror(char *s) { 62 | fprintf(stderr, "%s\n", s); 63 | exit(1); 64 | } -------------------------------------------------------------------------------- /src/yacc/startAndEndString/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickydebojeet/compiler-programs/20bf7ae2db4f3d064e00a414a1159a7a99e4ec39/src/yacc/startAndEndString/output.png -------------------------------------------------------------------------------- /src/yacc/startAndEndString/start.l: -------------------------------------------------------------------------------- 1 | /* lex program for recognition of string */ 2 | %{ 3 | #include 4 | #include 5 | #include 6 | /* Generated by yacc */ 7 | #include "y.tab.h" 8 | %} 9 | 10 | /* Lex definitions */ 11 | %% 12 | exit.* {return exit_command;} 13 | quit.* {return exit_command;} 14 | [01] {return yytext[0];} 15 | [a-zA-Z2-9]+ {return str;} 16 | \n {return newline;} 17 | [ \t] ; 18 | . {ECHO; yyerror("Unexpected Character");} 19 | %% 20 | int yywrap() { 21 | return 1; 22 | } -------------------------------------------------------------------------------- /src/yacc/startAndEndString/start.y: -------------------------------------------------------------------------------- 1 | /* yacc program for recognition of string */ 2 | %{ 3 | void yyerror(char *); 4 | #include 5 | #include 6 | %} 7 | 8 | /* yacc definations */ 9 | %start lines 10 | %token str 11 | %token newline 12 | %token exit_command 13 | 14 | /* Grammar Rules with actions */ 15 | %% 16 | lines: 17 | | lines S newline {;} 18 | | lines exit_command {exit(EXIT_SUCCESS);} 19 | ; 20 | S: '0' A '1' {printf("The string is detected\n>> ");} 21 | ; 22 | A: str {;} 23 | ; 24 | %% 25 | 26 | /* main function */ 27 | int main() { 28 | printf("String Chechker built using YACC and LEX\n"); 29 | printf("Reserved keywords: exit, quit\n"); 30 | printf(">> "); 31 | return yyparse(); 32 | } 33 | 34 | void yyerror(char *s) { 35 | fprintf(stderr, "%s\n", s); 36 | exit(1); 37 | } -------------------------------------------------------------------------------- /src/yacc/string/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickydebojeet/compiler-programs/20bf7ae2db4f3d064e00a414a1159a7a99e4ec39/src/yacc/string/output.png -------------------------------------------------------------------------------- /src/yacc/string/string.l: -------------------------------------------------------------------------------- 1 | /* lex program for recognition of string */ 2 | %{ 3 | /* Generated by yacc */ 4 | #include "y.tab.h" 5 | %} 6 | 7 | /* lex definitions */ 8 | %% 9 | exit.* {return exit_command;} 10 | quit.* {return exit_command;} 11 | [ba] {return yytext[0];} 12 | \n {return newline;} 13 | [ \t] ; 14 | . {ECHO; yyerror("Unexpected Character");} 15 | %% 16 | 17 | int yywrap() { 18 | return 1; 19 | } -------------------------------------------------------------------------------- /src/yacc/string/string.y: -------------------------------------------------------------------------------- 1 | /* yacc program for recognition of string */ 2 | %{ 3 | void yyerror(char *); 4 | #include 5 | #include 6 | %} 7 | 8 | /* yacc definations */ 9 | %start lines 10 | %token newline 11 | %token exit_command 12 | 13 | /* Grammar Rules with Actions */ 14 | %% 15 | lines: 16 | | lines S newline {;} 17 | | lines exit_command {exit(EXIT_SUCCESS);} 18 | ; 19 | S: A B {printf("The string is detected\n>> ");} 20 | ; 21 | A: A 'a' {;} 22 | | 'a' 'a' 'a' 'a' 'a' {;} 23 | ; 24 | B: 'b' 'b' {;} 25 | ; 26 | %% 27 | 28 | /* main function */ 29 | int main() { 30 | printf("String Chechker built using YACC and LEX\n"); 31 | printf("Reserved keywords: exit, quit\n"); 32 | printf(">> "); 33 | return yyparse(); 34 | } 35 | 36 | void yyerror(char *s) { 37 | fprintf(stderr, "%s\n", s); 38 | exit(1); 39 | } -------------------------------------------------------------------------------- /src/yacc/variableDetector/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rickydebojeet/compiler-programs/20bf7ae2db4f3d064e00a414a1159a7a99e4ec39/src/yacc/variableDetector/output.png -------------------------------------------------------------------------------- /src/yacc/variableDetector/variable.l: -------------------------------------------------------------------------------- 1 | /* lex program for recognition of valid variable */ 2 | %{ 3 | #include 4 | #include 5 | #include 6 | /* Generated by yacc */ 7 | #include "y.tab.h" 8 | %} 9 | IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* 10 | 11 | /* lex definitions */ 12 | %% 13 | exit.* {return exit_command;} 14 | quit.* {return exit_command;} 15 | {IDENTIFIER} {yylval.exp = strdup(yytext); return identifier;} 16 | \n {return newline;} 17 | [ \t] ; 18 | . {ECHO; yyerror("Unexpected Character");} 19 | %% 20 | 21 | int yywrap() { 22 | return 1; 23 | } -------------------------------------------------------------------------------- /src/yacc/variableDetector/variable.y: -------------------------------------------------------------------------------- 1 | /* yacc program for recognition of valid variable */ 2 | %{ 3 | void yyerror(char *); 4 | #include 5 | #include 6 | %} 7 | 8 | /* yacc defitions */ 9 | %union {char* exp;} 10 | %start lines 11 | %token identifier 12 | %token newline 13 | %token exit_command 14 | %type lines 15 | 16 | /* Grammar Rules with Actions */ 17 | %% 18 | lines: 19 | | lines identifier newline {printf("Valid Expression %s\n>> ", $2);} 20 | | lines exit_command {exit(EXIT_SUCCESS);} 21 | ; 22 | %% 23 | 24 | /* main function */ 25 | int main() { 26 | printf("Checks a valid variable\n"); 27 | printf("Reserved keywords: exit, quit\n"); 28 | printf(">> "); 29 | return yyparse(); 30 | } 31 | 32 | void yyerror(char *s) { 33 | fprintf(stderr, "%s\n", s); 34 | exit(1); 35 | } --------------------------------------------------------------------------------