Skip to content

Commit 8a7bd77

Browse files
Update README.md
1 parent a8c1426 commit 8a7bd77

File tree

1 file changed

+137
-124
lines changed

1 file changed

+137
-124
lines changed

labs/07/README.md

Lines changed: 137 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,139 @@
1-
# Lab 07 instructions
1+
# Practice Exercise: Building a Simple Chatbot with `lex` and `yacc`
22

3-
## Objective
4-
5-
Make the student create a CFG for the calculator previusly created using YACC
6-
7-
# Requirements
8-
9-
* Linux machine, either a VM or a bare metal host
10-
* GCC compiler
11-
* git send mail server installed and configured on your Linux machine
12-
* YACC
13-
14-
## Instructions
15-
Taking as imput the following code:
16-
17-
```
18-
$ cat FILE
19-
20-
// basic code
21-
22-
//float b
23-
f b
24-
25-
// integer a
26-
i a
27-
28-
// a = 5
29-
a = 5
30-
31-
// b = a + 3.2
32-
b = a + 3.2
33-
34-
//print 8.5
35-
p b
36-
```
37-
Reuse the code created in lab 04 to generate:
38-
39-
```
40-
$ cat tokens.out
41-
42-
floatdcl id
43-
intdcl id
44-
id assign inum
45-
id assign id plus fnum
46-
print id
47-
```
48-
IMPORTANT: calculator should accept + - * / as operator
49-
50-
51-
Before we reuse our code from lab 05 to:
52-
53-
* Detect nonterminals that cannot be reached from a CFG’s goal symbol.
54-
* Detect nonterminals that cannot derive any terminal string in a CFG.
55-
56-
Since we already did this in HW 06 is not necessary to do it again
57-
58-
## How could it be tested:
59-
```
60-
61-
make ( compile everything )
62-
63-
./lexic_analyzer <CODEFILE>
64-
65-
```
66-
This will generate the tokens.out
67-
68-
Use then for generate the parse tree:
69-
70-
```
71-
./syntax-calc tokens.out
72-
```
73-
And generate a CFG derivation tree in this format:
74-
75-
```
76-
digraph D {
77-
78-
A -> {B, C, D} -> {F}
79-
80-
}
81-
82-
83-
IMPORTANT: Use this code as base
84-
85-
https://avinashsuryawanshi.files.wordpress.com/2016/10/9.pdf
86-
87-
It will give you a basic example of how to make the AST
88-
89-
```
90-
91-
View of this tree, put this code in:
92-
93-
https://dreampuf.github.io/GraphvizOnline
94-
95-
More info about DOT code:
96-
97-
https://renenyffenegger.ch/notes/tools/Graphviz/examples/index
98-
99-
## Please send the mail as git send mail:
100-
101-
```
102-
$ git add syntax-calc.c lexic_analyzer.c Makefile
103-
$ git commit -s -m <STUDENT-ID>-homework-07
104-
$ git send-email -1
105-
106-
```
107-
108-
Or if you use YACC
109-
110-
111-
```
112-
$ git add syntax-calc.y lexic_analyzer.l Makefile
113-
$ git commit -s -m <STUDENT-ID>-homework-07
114-
$ git send-email -1
115-
116-
```
117-
118-
Do some tests sending the mail to your personal
119-
account, if you get the mail, then you can be sure I
120-
will get the mail
121-
122-
123-
## Time to do the homework:
124-
125-
One week from the moment the mail is sent to students
3+
#### Objective
4+
In this exercise, you will create a simple chatbot using `lex` (for lexical analysis) and `yacc` (for parsing). The chatbot will be able to respond to greetings, queries about the time, and farewells. This practice will help you understand how to use `lex` and `yacc` to build a basic interactive application.
5+
6+
#### Prerequisites
7+
- Basic understanding of C programming.
8+
- Familiarity with `lex` and `yacc` tools.
9+
- Basic knowledge of lexical analysis and parsing.
12610

11+
#### Instructions
12+
13+
1. **Setup Your Environment**:
14+
- Ensure you have `lex` (or `flex`) and `yacc` (or `bison`) installed on your system.
15+
- Create a working directory for this exercise.
16+
17+
2. **Create the Lex Specification**:
18+
- Create a file named `chatbot.l`.
19+
- Define patterns to match user inputs for greetings, farewells, and time queries.
20+
21+
```c
22+
%{
23+
#include "y.tab.h"
24+
%}
25+
26+
%%
27+
28+
hello { return HELLO; }
29+
hi { return HELLO; }
30+
hey { return HELLO; }
31+
goodbye { return GOODBYE; }
32+
bye { return GOODBYE; }
33+
time { return TIME; }
34+
what[' ']is[' ']the[' ']time { return TIME; }
35+
what[' ']time[' ']is[' ']it { return TIME; }
36+
\n { return 0; } /* End of input on newline */
37+
38+
. { return yytext[0]; }
39+
40+
%%
41+
42+
int yywrap() {
43+
return 1;
44+
}
45+
```
46+
47+
3. **Create the Yacc Specification**:
48+
- Create a file named `chatbot.y`.
49+
- Define grammar rules to handle different types of user inputs.
50+
51+
```c
52+
%{
53+
#include <stdio.h>
54+
#include <time.h>
55+
56+
void yyerror(const char *s);
57+
int yylex(void);
58+
%}
59+
60+
%token HELLO GOODBYE TIME
61+
62+
%%
63+
64+
chatbot : greeting
65+
| farewell
66+
| query
67+
;
68+
69+
greeting : HELLO { printf("Chatbot: Hello! How can I help you today?\n"); }
70+
;
71+
72+
farewell : GOODBYE { printf("Chatbot: Goodbye! Have a great day!\n"); }
73+
;
74+
75+
query : TIME {
76+
time_t now = time(NULL);
77+
struct tm *local = localtime(&now);
78+
printf("Chatbot: The current time is %02d:%02d.\n", local->tm_hour, local->tm_min);
79+
}
80+
;
81+
82+
%%
83+
84+
int main() {
85+
printf("Chatbot: Hi! You can greet me, ask for the time, or say goodbye.\n");
86+
while (yyparse() == 0) {
87+
// Loop until end of input
88+
}
89+
return 0;
90+
}
91+
92+
void yyerror(const char *s) {
93+
fprintf(stderr, "Chatbot: I didn't understand that.\n");
94+
}
95+
```
96+
97+
4. **Compile the Lex and Yacc Files**:
98+
- Open a terminal in your working directory.
99+
- Run the following commands to compile the lex and yacc files:
100+
101+
```sh
102+
lex chatbot.l
103+
yacc -d chatbot.y
104+
cc lex.yy.c y.tab.c -o chatbot -ll -ly
105+
```
106+
107+
5. **Run the Chatbot**:
108+
- Execute the compiled chatbot program:
109+
110+
```sh
111+
./chatbot
112+
```
113+
114+
- Test the chatbot by typing various inputs:
115+
- Greetings like "hello", "hi", or "hey".
116+
- Time queries like "what is the time", "what time is it", or simply "time".
117+
- Farewells like "goodbye" or "bye".
118+
119+
6. **Extend the Chatbot**:
120+
- Add more patterns and responses to the chatbot. Think of additional questions users might ask and how the chatbot should respond. For example:
121+
- Ask for the chatbot's name: "what is your name".
122+
- Inquire about the weather: "what is the weather".
123+
- Ask how the chatbot is doing: "how are you".
124+
125+
126+
#### Submission
127+
Create a pull request and submit the following files:
128+
- `chatbot.l`
129+
- `chatbot.y`
130+
131+
Ensure your code is well-commented to explain your logic and any enhancements you made.
132+
133+
#### Assessment
134+
You will be evaluated on:
135+
- Correctness of the lexical and grammar rules.
136+
- Functionality of the chatbot based on the provided and additional commands.
137+
- Code readability and comments.
138+
139+
By completing this exercise, you will gain practical experience in using `lex` and `yacc` to build and extend a basic interactive application, laying the foundation for more complex projects in the future.

0 commit comments

Comments
 (0)