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
2 changes: 2 additions & 0 deletions 1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const x=8,y=7;var a,b,c;begin a=2*x;;b=a+x+y;;c=a+3*b;end
const x=8,y=7;var a,b,c;begin a=b+x;;if a>0 then begin c=y -1;;a=a+2;end else begin c=a+y;end;while a>0 do a=a-1;end
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ using namespace std;
#define ID 15
#define NUM 16

//��Ŵ�������ַ���

char tempstr[255] = {};
//�ո��־

bool temp = false;
//��ʱ����

char word[255] = {};
//keyword�ؼ���

string keyword[9] = {
"Const", "Var", "if", "else", "then",
"while", "do", "begin", "end"
Expand All @@ -23,28 +23,29 @@ int keyword_num[9] = {
1,2,3,4,5,
6,7,8,9
};
//������������������
char symbol[9] = { '+','-','*','/',';','(',')','{','}'};
//��Ӧ������ֵ

char symbol[9] = { '+', '-', '*', '/', ';', '(' ,')' ,'{' ,'}'};

int symbol_num[9] = { 21,22,23,24,25,26,27,28,29 };

//�ж��Ƿ�Ϊ��ĸ
bool err_flag = false;

bool IsLetter(char ch)
{
if ((ch >= 'a'&&ch <= 'z') || (ch >= 'A'&&ch <= 'Z'))
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
return true;
return false;
}

//�ж��Ƿ�Ϊ����

bool IsDigit(char ch)
{
if (ch >= '0'&&ch <= '9')
return true;
return false;
}

//�ж��Ƿ�Ϊ�������

int IsSymbol(char ch)
{
for (int i = 0; i<9; i++)
Expand All @@ -55,7 +56,7 @@ int IsSymbol(char ch)
return -1;
}

//�ж��Ƿ�Ϊ�ؼ���

int IsKeyword(string str)
{
for (int i = 0; i<26; i++)
Expand All @@ -65,15 +66,15 @@ int IsKeyword(string str)
return i;
}
}
//���ǹؼ��ּ�ΪID

return ID;
}

//�ո���

void HandleSpace(char a[])
{
int j = 0;
memset(word, 0, 255);//��Ҫ��գ���Ȼ���ܲ����ϴε��ַ���
memset(word, 0, 255);
temp = false;
for (int i = 0; i < strlen(a); i++)
{
Expand All @@ -94,7 +95,7 @@ void HandleSpace(char a[])
}
}

//����"//"ע��

void prePro()
{
int j = 0;
Expand All @@ -119,45 +120,48 @@ void Scanner(char *str);

int main()
{
char instr[255] = {}; //���������ַ���
bool flag = false; //����ע�ͱ�־,falseΪδ����ע������
string Token;//����ַ���
char *str = NULL;//���ÿ�е��ַ���
char delims[] = " ";//�ָ��־
char instr[255] = {};
bool flag = false;
string Token;
char *str = NULL;
char delims[] = " ";
freopen("test.cpp", "r", stdin);
freopen("result.txt", "w", stdout);

printf("�ʷ�����������£�\n");
printf("******************************\n");

while ((gets_s(instr)) != NULL)
while ((gets(instr)) != NULL)
{
HandleSpace(instr);
prePro();

str = strtok(tempstr, delims);//�ָ��ַ���
str = strtok(tempstr, delims);

while (str != NULL)
{
//ͷ�ļ����궨��

if (*(str) == '#')
{
printf("#\n");
break;
}

Scanner(str);

if (err_flag) {
break;
}
str = strtok(NULL, delims);
}
if(err_flag) break;
}

return 0;
}

void Scanner(char *str) {
bool flag = false; //����ע�ͱ�־,falseΪδ����ע������
string Token;//����ַ���
bool flag = false;
string Token;
int type = 0;

for (int i = 0; i<strlen(str); i++)
Expand All @@ -170,8 +174,8 @@ void Scanner(char *str) {
break;
}
}
//ע�ʹ���: */,ע���������
if (*(str + i) == '*'&&flag)

if (*(str + i) == '*' && flag)
{
if (*(str + i + 1) == '/')
{
Expand All @@ -181,7 +185,6 @@ void Scanner(char *str) {
}
}

//��ʶ�����ؼ���
if (IsLetter(*(str + i)) && (!flag))
{
while (IsLetter(*(str + i)) || IsDigit(*(str + i)) || *(str + i) == '_')
Expand All @@ -204,14 +207,28 @@ void Scanner(char *str) {

if (IsDigit(*(str + i)) && (!flag))
{

while (IsDigit(*(str + i)))
{
Token += *(str + i);
if(IsLetter(*(str + i + 1))) {
int tempIndex = 1;
while(IsLetter(*(str + i + tempIndex))) {
Token += *(str + i + tempIndex);
tempIndex += 1;
}
printf("<error: %s wrong identifier>", Token.c_str());
Token = "";
err_flag = true;
break;
}
i++;
}
if (err_flag) break;
type = NUM;
printf("< %s , %d >\n", Token.c_str(), type);
Token = "";

}

//<,<=,<>
Expand Down
94 changes: 94 additions & 0 deletions CPlab2_LexicalAnalysis/LexicalAnalysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
def scan(string):
KEY_WORDS = ["Const", "Var", "if", "else", "then","while", "do", "begin", "end"]
SYMBOLS = [',', '+', '-', '*', '/', ';', '(' ,')' ,'{' ,'}', '==', '<=', '>=', '<>', '<', '>', '=']
word_map = {key: i+1 for i, key in enumerate(KEY_WORDS)}
word_map.update({key: i+21 for i, key in enumerate(SYMBOLS)})
def process(string):
word = ''
if ' ' in string:
for s in string.split(' '):
word += s
else:
word = string
if '//' in word:
return word.split('//')[0]
return word

def have_keywords(word):
for keyword in KEY_WORDS:
if keyword in word:
return keyword
return None

def have_symbol(word):
for sy in SYMBOLS:
if sy in word:
return sy
return None

def is_alpha(ch):
return (ch >= 'a' and ch <= 'z') or (ch >= 'A' and ch <= 'Z')

def is_digit(ch):
return ch >= '0' and ch <= '9'

# def is_in_symbols(c):
# return c in SYMBOLS

# def is_keywords(c):
# return c in KEY_WORDS


queue = [process(string)]
if '/*' in queue[0] or '*/' in queue[0]:
return
stop = False
while not stop:
changed = False
tempQueue = []
for item in queue:
key = have_keywords(item)
sy = have_symbol(item)
if key and key != item:
changed = True
item1, item2 = item.split(key)
if item1:
tempQueue.append(item1)
tempQueue.append(key)
if item2:
tempQueue.append(item2)
continue
elif sy and sy != item:
changed = True
sp = item.split(sy)
for i, it in enumerate(sp):
if it:
tempQueue.append(it)
if i != len(sp) - 1:
tempQueue.append(sy)

else:
tempQueue.append(item)

if item == queue[-1] and not changed:
stop = True
queue = tempQueue.copy()
for item in queue:
if item in word_map:
print("<", item, ",", word_map[item],">")
else:
if item and is_alpha(item[0]):
print("<", item, ",", 15,">")
if item and is_digit(item[0]):
print("<", item, ",", 16,">")

def main():
with open('./test.cpp', 'r') as r_stream:
for line in r_stream:
if line:
if '\n' in line:
scan(line[0:-1])
else:
scan(line)

main()
Binary file added CPlab2_LexicalAnalysis/a.out
Binary file not shown.
26 changes: 26 additions & 0 deletions CPlab2_LexicalAnalysis/result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
�ʷ�����������£�
******************************
#
< int , 15 >
< main , 15 >
< ( , 26 >
< ) , 27 >
< { , 28 >
< Const , 1 >
< const_a , 15 >
< = , 35 >
< 10 , 16 >
< ; , 25 >
< Var , 2 >
< var_b , 15 >
< = , 35 >
< 15 , 16 >
< ; , 25 >
< if , 3 >
< ( , 26 >
< const_a , 15 >
< > , 33 >
< 5 , 16 >
< ) , 27 >
< { , 28 >

19 changes: 19 additions & 0 deletions CPlab2_LexicalAnalysis/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Const x=8,y=7;
Var a,b,c;
begin
a=b+x;

if a>0
then
begin
c=y -;
a=a+2;
end
else
begi
c=a+y;
end

while a>0
do a=a-1;
end
3 changes: 3 additions & 0 deletions CPlab2_LexicalAnalysis/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
a = 'adsd;'
if 'adsd;' in a:
print('vfs')
Loading