Đề số 2

You might also like

You are on page 1of 4

Đề 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();
}

You might also like