diff --git a/labs/04/README.md b/labs/04/README.md index 43d601d..4d5bd81 100644 --- a/labs/04/README.md +++ b/labs/04/README.md @@ -1,27 +1,49 @@ -# Lab 04 instructions +# Activity 3.1: Lexical Analyzer (SCANNER) -## Objective +Author: Emilio Berber Maldonado A01640603 -Make the student understand the power of lex language making a C code that -performs the lexical analysis of the ac src program +## Purpose +The objective of this activity is to develop a basic lexical analyzer (scanner) using Flex. This analyzer processes a source code file and outputs the corresponding tokens, recognizing keywords, identifiers, numbers, operators, unknown digits and cooments. -# Requirements +## Requirements +- Install Flex (Fast Lexical Analyzer Generator) +- Install GCC or another C compiler +- Unix-like environment or terminal (Linux, macOS, or Git Bash/MSYS2 for Windows) -* Linux machine, either a VM or a bare metal host -* GCC compiler (at least version 4.8) -* lex compiler -* Autotools -* git send mail server installed and configured on your Linux machine +## Files +- `lex_analaizer.l`: The Flex definition file containing the lexical rules +- `example.ac`: Sample input file with code to analyze +- `lex_analyzer.exe`: Compiled executable generated from the `.l` file (on Windows) +- lex.yy.c -## Instructions +## How to Compile and Run -Please generate a LEX code to parse the previous example of lab 03. +1. Generate the C source from the `.l` file: + ```bash + flex lex_analaizer.l + ``` -A valid line of code in ac could be: +2. Compile the generated `lex.yy.c` file: + ```bash + gcc lex.yy.c -o lex_analyzer + ``` +3. Run the analyzer with your test input: + ```bash + ./lex_analyzer example.ac + ``` +## Output Format +Each recognized token is printed to standard output on a separate line. Comments and unknown characters are also reported as `COMMENT` and `UNKNOWN`, respectively. Whitespace and line breaks are ignored in tokenization but respected in output formatting for clarity. + +## Example + +### Input: (`example.ac`) ``` +a = 3 +// Emilio Berber +// Mi matrĂ­cula es: A01640603 +h = 123131312321312 // basic code - //float b f b @@ -29,18 +51,22 @@ f b i a // a = 5 -a = 5 +a = 5 // b = a + 3.2 b = a + 3.2 -//print 8.5 -p b +// print 8.5 +p 8.5 +? ``` -Your output should be - +### Output: ``` +id assign inum +COMMENT +COMMENT +id assign inum COMMENT COMMENT floatdcl id @@ -51,45 +77,20 @@ id assign inum COMMENT id assign id plus fnum COMMENT -print id -``` - -## Expected result: - -* Code a lex_analaizer.L that fulfill the requirements -* Generate a random AC code with: - -``` -python3 code_generator.py > example.ac - +print fnum +UNKNOWN ``` -* Compile your code with the makefile and execute as follows: - -``` -./lex_analaizer example.ac -``` - - -## Please send the mail as PR: - -``` - $ git add lex_analaizer.l - $ git commit -s -m -homework-04 -``` -Do some tests sending the mail to your personal account, if you get the mail, -then you can be sure I will get the mail - -## Good links for Hints - -* [lextutorial](https://ds9a.nl/lex-yacc/cvs/lex-yacc-howto.html) -* [lex & yacc Second -Edition](https://www.amazon.com/lex-yacc-Doug-Brown/dp/1565920007) -At the end of chapter 1 there is a very similar code as the one requested in -this homework, you just need to read chapter 1 of this book :) -* [useoflexinc](https://www.quora.com/What-is-the-function-of-yylex-yyin-yyout-and-fclose-yyout-in-LEX) - -## Time to do the homework: - -One week from the moment the mail is sent to students - +## Recognized Tokens +| Token | Description | +|------------|------------------------------------| +| `floatdcl` | Float declaration (`f`) | +| `intdcl` | Integer declaration (`i`) | +| `print` | Print statement (`p`) | +| `id` | Identifier (variable name) | +| `assign` | Assignment operator (`=`) | +| `plus` | Addition operator (`+`) | +| `inum` | Integer number | +| `fnum` | Floating point number | +| `COMMENT` | Line comment (starts with `//`) | +| `UNKNOWN` | Any unrecognized symbol (e.g., `?`)| \ No newline at end of file diff --git a/labs/04/example.ac b/labs/04/example.ac new file mode 100644 index 0000000..2735c8b --- /dev/null +++ b/labs/04/example.ac @@ -0,0 +1,25 @@ +a = 3 +// Emilio Berber +// Mi matrĂ­cula es: A01640603 +h = 123131312321312 +// basic code +//float b +f b + + + +// integer a +i a + +// a = 5 +a = 5 + +// b = a + 3.2 +b = a + 3.2 + +// print 8.5 +p 8.5 + +? + + diff --git a/labs/04/lex_analaizer.l b/labs/04/lex_analaizer.l new file mode 100644 index 0000000..5090371 --- /dev/null +++ b/labs/04/lex_analaizer.l @@ -0,0 +1,39 @@ +%{ +#include +%} + + +%% + +\/\/.* { printf("COMMENT\n"); } +"f" { printf("floatdcl "); } +"i" { printf("intdcl "); } +"p" { printf("print "); } +"=" { printf("assign ");} +"+" { printf("plus ");} +[0-9]+ { printf("inum \n");} +[0-9]*"."[0-9]+ { printf("fnum \n"); } +[a-zA-Z]+ { printf("id ");} +[ \t\n]+ /* ignore whitespace and line breaks */; +. { printf("UNKNOWN \n"); } + + +%% + + +int main(int argc, char *argv[]) { + if (argc > 1) { + FILE *file = fopen(argv[1], "r"); + if (!file) { + perror("Error to open the file"); + return 1; + } + yyin = file; // Flex + } + yylex(); + return 0; +} + +int yywrap() { + return 1; +}