You are on page 1of 10

Đề số 2

Bài 1: Cho tập sản xuất:


S → ABc
A → aD | ε
B → bB | DA
D → aDb | ε
a) Lập bảng tính FIRST và FOLLOW:
+) Bảng tính FIRST:

X F0(X) F1(X) F2(X) F3(X) FIRST(X)


S ∅ a,b a,b,c a,b,c a,b,c
A a,ε a,ε a,ε
B b b,a,ε b,a,ε a,b,ε
D a,ε a,ε a,ε

+) Bảng tính FOLLOW:

Quy tắc áp Sản xuất áp FOLLOW


dụng dụng S A B D
1 Khởi tạo $
S → ABc a,b c
2 B → DA a
D → aDb b
A → aD a,b
B → DA c
3
B → DA
c
A→ε
FOLLOW(X) $ a,b,c c a,b,c

b) Bảng phân tích tất định LL(1):

Ký hiệu cú Ký hiệu vào


pháp a b c $
S S → ABc S → ABc S → ABc
A → aD
A A→ε A→ε
A→ε
B B → DA B → bB B → DA
D → aDb
D D→ε D→ε
D→ε

c) Phân tích LL với xâu vào aabbc theo dạng:

Ngăn xếp Đầu vào Đầu ra


$S aabbc$
$cBA aabbc$ S → ABc
$cBDa aabbc$ A → aD
$cBD abbc$
$cBbDa abbc$ D → aDb
$cBbD bbc$
$cBb bbc$ D→ε
$cB bc$
$cBb bc$ B → bB
$cB c$
$cAD c$ B → DA
$cA c$ D→ε
$c c$ A→ε
$ $

Bài 2: Viết lại một phần chương trình phân tích từ vựng cho ngôn ngữ SLANG với
yêu cầu như sau: Sửa lại phần khai báo từ tố, khai báo từ khóa và viết lại hàm
next_token() chỉ cho câu lệnh IF (không quan tâm đến các câu lệnh trong thân của
IF). Phần <quan hệ> của IF chỉ có các phép so sánh: >, >=, =. Phần <biểu thức>
trong <quan hệ> chỉ có các phép tính cộng, trừ. Phần <toán hạng> trong <biểu thức>
chỉ có tên và số.

struct keyword keywtb[]={


{if_token,”IF”}
};

char ch;
boolean retract;
token look_ahead;
string ident_lexeme;
int number_value;

void next_token(void)
{
retract = false;
while( (ch <= ‘ ‘) || (ch == ‘{‘) && (ch != EOF) ){
if(ch == ‘{‘) coment();
next_character();
}
if(ch == EOF) return;
switch(ch){
case ‘(’ : look_ahead = open_token; break;
case ‘)’ : look_ahead = close_token; break;
case ‘,’ : look_ahead = list_token; break;
case ‘;’ : look_ahead = separator_token; break;
case ‘+’: look_ahead = plus_token; break;
case ‘-’ : look_ahead = minus_token; break;
case ‘=’: look_ahead = equal_token; break;
case ‘>’ : next_character();
if(ch == ‘=’) look_ahead = greater_or_equal_token;
else{
look_ahead = greater_than_token;
retract = true;
}
break;
default :
if(isalpha(ch)) indentifier();
else if(isdigit(ch)) number();
}
if(!retract) next_character();
}

Bài 3: Viết các hàm sau của chương trình phân tích cú pháp cho ngôn ngữ SLANG:
statement_part(), statement(), assignment_statement(), left_part().

void statement_part(void)
{
statement();
while(look_ahead == separator_token){
next_token();
statement();
}
}

void statement(void)
{
if(look_ahead == ident_token) assignment_statement();
else if(look_ahead == if_token) if_statement();
else if(look_ahead == while_token) while_statement();
else if(look_ahead == call_token) call_statement();
else if(look_ahead == read_token) read_statement();
else if(look_ahead == write_token) write_statement();
}

void assignment_statement(void)
{
left_part();
if(look_ahead == becomes_token) next_token();
expression();
}

void left_part(void)
{
if(look_ahead == ident_token) next_token();
}

Đề số 3
Bài 2: Trong chương trình phân tích từ vựng cho ngôn ngữ SLANG,
hãy:
a) Trình bày công dụng của hàm identifier() và biến ident_lexeme, viết
nội dung hàm identifier().
b) Trình bày công dụng của hàm number() và biến num_value, viết
nội dung hàm number().
c) Trình bày công dụng của hàm look_up().

a)
-Công dụng hàm identifier(): Phân tích tên và kiểm tra xem có phải là từ khóa không.
Trả về từ tố ident_token hoặc từ tố theo từ khóa, xâu tên được đặt trong xâu
ident_lexeme.
-Công dụng biến ident_lexeme: Nếu từ tố này là một từ khóa (keyword) hoặc là một
tên (ident_token) thì nó sẽ đặt tên này trong xâu ident_lexeme.
void identifier(void)
{
int index=0;
do{
ident_lexeme[index] = ch;
index++;
next_character();
}while(isalpha(ch) || isdigit(ch));
ident_lexeme[index] = 0;
look_ahead = look_up(ident_lexeme);
if(look_ahead == err_token) look_ahead = ident_token;
retract=true;
}
b)
-Công dụng hàm number(): Đoán nhận số. Hàm này cũng tính luôn giá trị của số đó
và đặt vào biến num_value.
-Công dụng biến num_value: Nếu từ tố này là số (num_token) thì nó sẽ đặt giá trị của
số đó trong biến num_value.
void number(void)
{
num_value = 0;
do{
num_value = num_value * 10+ ch – ‘0’;
next_character();
} while(isdigit(ch));
look_ahead=num_token;
retract=true;
}
c) Công dụng hàm look_up(): Trả lại từ tố của từ khóa đang nằm trong biến
ident_lexeme.
Bài 3: Viết các hàm sau của chương trình phân tích cú pháp cho ngôn
ngữ SLANG:
if_statement(), while_statement(), call_statement(), read_statement().

void if_statement(void)
{
if(look_ahead == if_token) next_token();
relation();
if(look_ahead == then_token) next_token();
statement_part();
if(look_ahead == else_token){
next_token();
statement_part();
}
if(look_ahead == fi_token) next_token();
}

void while_statement(void)
{
if(look_ahead == while_token) next_token();
relation();
if(look_ahead == do_token) next_token();
statement_part();
if(look_ahead == od_token) next_token();
}

void call_statement(void)
{
if(look_ahead == call_token) next_token();
if(look_ahead == ident_token) next_token();
}

void read_statement(void)
{
if(look_ahead == read_token) next_token();
if(look_ahead == open_token) next_token();
if(look_ahead == ident_token) next_token();
while(look_ahead == list_token){
next_token();
if(look_ahead == ident_token) next_token();
}
if(look_ahead == close_token) next_token();
}

Đề số 4
Bài 1: Cho tập sản xuất:
S → BAa
A → cA | ε
B → aB | AE
E → bcE | ε

a) Lập bảng tính FIRST và FOLLOW:


+) Bảng tính FIRST:

X F0(X) F1(X) F2(X) F3(X) FIRTS(X)


S ∅ a a,c,b a,c,b a,b,c
A c,ε c,ε c,ε
B a a,c,b,ε a,c,b,ε a,b,c,ε
E b,ε b,ε b,ε
+) Bảng tính FOLLOW:

Quy tắc áp Sản xuất áp FOLLOW


dụng dụng S A B E
1 Khởi tạo $
S → BAa a c
2
B → AE b
B → AE c
3 B → AE
c
E→ε
FOLLOW(X) $ a,b,c c c

b) Bảng phân tích tất định LL(1):

Ký hiệu cú Ký hiệu vào


pháp a b c $
S S → BAa S → BAa S → BAa
A → cA
A A→ε A→ε
A→ε
B B → aB B → AE B → AE B → AE
E E → bcE E→ε

c) Phân tích LL với xâu vào abcca theo dạng:

Ngăn xếp Đầu vào Đầu ra


$S abcca$
$aAB abcca$ S → BAa
$aABa abcca$ B → aB
$aAB bcca$
$aAEA bcca$ B → AE
$aAE bcca$ A→ε
$aAEcb bcca$ E → bcE
$aAEc cca$
$aAE ca$
$aA ca$ E→ε
$aAc ca$ A → cA
$aA a$
$a a$ A→ε
$ $

Bài 2: Viết lại một phần chương trình phân tích từ vựng cho ngôn ngữ
SLANG với yêu cầu như sau: Sửa lại phần khai báo từ tố, khai báo từ
khóa và viết lại hàm next_token() chỉ cho câu lệnh WHILE (không
quan tâm đến các câu lệnh trong thân của WHILE). Phần <quan hệ>
của WHILE chỉ có các phép so sánh: <, <=, <>. Phần <biểu thức>
trong <quan hệ> chỉ có các phép tính nhân, chia. Phần <toán hạng>
trong <biểu thức> chỉ có tên và số.

struct keyword keywtb[]={


{while_token,”WHILE”}
};

char ch;
boolean retract;
token look_ahead;
string ident_lexeme;
int number_value;

void next_token(void)
{
retract = false;
while( (ch <= ‘ ‘) || (ch == ‘{‘) && (ch != EOF) ){
if(ch == ‘{‘) coment();
next_character();
}
if(ch == EOF) return;
switch(ch){
case ‘(’ : look_ahead = open_token; break;
case ‘)’ : look_ahead = close_token; break;
case ‘,’ : look_ahead = list_token; break;
case ‘;’ : look_ahead = separator_token; break;
case ‘*’: look_ahead = times_token; break;
case ‘/’ : look_ahead = over_token; break;
case ‘<’ : next_character();
if(ch == ‘>’) look_ahead = not_equal_token;
else if(ch == ‘=’) look_ahead = less_or_equal_token;
else{
look_ahead = less_than_token;
retract = true;
}
break;
default :
if(isalpha(ch)) indentifier();
else if(isdigit(ch)) number();
}
if(!retract) next_character();
}

Bài 3: Viết lại các hàm sau của chương trình phân tích cú pháp cho
ngôn ngữ SLANG:
expression(), term(), factor(), operand().

void expression(void)
{
term();
while(look_ahead == minus_token || look_ahead == plus_token){
add_operator();
term();
}
}

void term(void){
{
factor();
while(look_ahead == modulo_token || look_ahead == over_token || look_ahead
|| look_ahead == times_token){
multiply_operator();
factor();
}
}

void factor(void)
{
if(look_ahead == absolute_token || look_ahead == negate_token){
unary_operator();
}
operand();
}
void operand(void)
{
if(look_ahead == ident_token) next_token();
else if(look_ahead == num_token) next_token();
else if(look_ahead == open_token){
next_token();
expression();
if(look_ahead == close_token) next_token();
}
}

You might also like