This repository contains 12 programs written using Lex and YACC, labeled as 1a
, 1b
, 2a
, 2b
, 3a
, 3b
, 4a
, 4b
, 5
, 6
, 7
, and 8
. Each program demonstrates specific functionalities and parsing tasks, with some involving both Lex and YACC.
To build and run the programs, ensure you have the following tools installed:
lex
(orflex
)yacc
(orbison
)- A C compiler (e.g.,
gcc
)
For each program:
-
Navigate to the respective directory (e.g.,
cd 1a
). -
Run the following commands:
lex <filename>.l gcc lex.yy.c -o <output> ./<output>
yacc -d <filename>.y lex <corresponding Lex file>.l gcc y.tab.c lex.yy.c -o <output> ./<output>
- 1a.l: Counts the number of words, lines, characters, and whitespaces in the input.
- 1b.l & 1b.y: Validates input strings based on defined grammar rules.
- 2a.l: Counts positive/negative integers and fractions from the input.
- 2b.l & 2b.y: Evaluates arithmetic expressions.
- 3a.l: Removes comments from a C program and counts the total number of comments.
- Input (3a_ip.txt): A C program with single-line and multi-line comments.
- Output (3a_op.txt): The same program with all comments removed.
- 3b.l & 3b.y: Parses and validates nested
for
loops and counts them.
- 4a.l: Processes arithmetic expressions in a C-style code snippet.
- Counts keywords (
int
,float
,char
), identifiers, and operators.
- Counts keywords (
- Input (4a_ip.txt): C code snippet with declarations and arithmetic operations.
- 4b.l & 4b.y: Parses and validates nested
if
statements and counts them.
- 5.l & 5.y: Tokenizes type declarations, identifiers, and arrays, and parses declarations to count total variables.
- Input (5_input.txt): Sample type declarations with variables and arrays.
- 6.l & 6.y: Tokenizes mathematical expressions with variables and numbers, and generates three-address code and quadruple code for expressions.
- 7.l & 7.y: Tokenizes components of a C program, ignoring comments, and parses and validates the structure of C programs, including functions and statements.
- Input (7_input.txt): A sample C program demonstrating declarations, statements, and comments.
- 8.l & 8.y: Tokenizes components for assembly-like code generation and generates assembly-like code for simple C programs with arithmetic and
printf
statements.
Feel free to contribute by opening issues or submitting pull requests to enhance the repository.
This project is open-source and available under the MIT License.