Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 60 additions & 59 deletions labs/04/README.md
Original file line number Diff line number Diff line change
@@ -1,46 +1,72 @@
# 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

// integer a
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
Expand All @@ -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 <STUDENT-ID>-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., `?`)|
25 changes: 25 additions & 0 deletions labs/04/example.ac
Original file line number Diff line number Diff line change
@@ -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

?


39 changes: 39 additions & 0 deletions labs/04/lex_analaizer.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
%{
#include <stdio.h>
%}


%%

\/\/.* { 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;
}