This repository contains the solution for the second exercise of the Compiler course. The exercise involved creating two versions of a parser: one using Flex & Bison and the other using Flex & Recursive Descent Parser.
olympics.lex: The Flex file containing the lexical analyzer definitions.olympics.y: The Bison file containing the parser definitions for the bottom-up approach.olympics.c: The C file containing the parser definitions for the top-down approach.test_olympics.txt: A sample input file with data on Olympic sports and the years they were part of the games.Makefile: A Makefile to compile the Flex and Bison code and run the parsers on the sample input.
To compile and run the lexical analyzer and parsers, follow these steps:
-
Generate the C source files using Flex and Bison:
flex olympics.lex bison -d olympics.y -
Compile the generated C source files:
gcc -o olympics_bison lex.yy.c olympics.tab.c -
Run the parser with the sample input:
./olympics_bison test_olympics.txt
-
Generate the C source file using Flex:
flex olympics.lex -
Compile the generated C source file along with the parser file:
gcc -o olympics_recursive lex.yy.c olympics.c -
Run the parser with the sample input:
./olympics_recursive test_olympics.txt
Another Option is to Open the terminal in a desired folder and run this commands:
-
make -
make run
The parsers will output the names of sports that appeared in at least 7 Olympic games and the average number of games per sport. For example:
sports which appeared in at least 7 olympic games: Archery Athletics Basketball
average number of games per sport: 15.60
The input format and token types are detailed in the exercise description. Refer to the olympics.lex, olympics.y, and olympics.c files for the specific regular expressions and grammar rules used to match each token type.
Ensure Flex, Bison, and GCC are installed on your system to compile and run the parsers.
This project was completed as part of the Compiler course in June 2024.
