Skip to content

Commit

Permalink
merge wdy
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeeef committed Jun 4, 2019
2 parents bf0462b + d6701a8 commit 85c34f8
Show file tree
Hide file tree
Showing 9 changed files with 3,157 additions and 139 deletions.
482 changes: 482 additions & 0 deletions yapc/doc/report.md

Large diffs are not rendered by default.

Binary file removed yapc/graph.png
Binary file not shown.
2,427 changes: 2,427 additions & 0 deletions yapc/parselog.txt

Large diffs are not rendered by default.

267 changes: 138 additions & 129 deletions yapc/parsetab.py

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions yapc/test/function.pas
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
var A : integer;
begin
A := 10;
writeln(A)
writeln(A) ;
end;

function Summation(num : integer) : integer;
Expand All @@ -24,4 +24,4 @@
A := a + Summation(10);
writeln(a);
writeln(A);
end.
end.
11 changes: 11 additions & 0 deletions yapc/test/switch.pas
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
begin
week := '7';

<<<<<<< Updated upstream
case ( week) of
'1' : writeln('M');
'2' : writeln('T');
Expand All @@ -11,6 +12,16 @@
'5' : writeln('F');
'6' : writeln('S');
'7' : writeln('S');
=======
case (week) of
'1' : writeln('Monday');
'2' : writeln('Tuesday');
'3' : writeln('Wednesday');
'4' : writeln('Thursday');
'5' : writeln('Friday');
'6' : writeln('Satursday');
'7' : writeln('Sunday');
>>>>>>> Stashed changes
end;
writeln(week);
end.
40 changes: 40 additions & 0 deletions yapc/test/test_error.pas
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
program test_1;
const a = 2; b = 3.4; c = 'l';
type
arr = array [0..50] of integer;
MailingListRecord = record
FirstName: strig;
LastName: string;
Address: string;
City: string;
State: string;
Zip: Integer;
end;
var i,a,b,k,x,y sum,testfunc : integer;
aa:arr;
function fib(x:integer):integer;
begin
if ((x = 0) or (x = 1)) then
fib:=1
else
fib:=fib(x - 2) + fib(x - 1);
end;

function gao(x:integer):integer;
begin
gao:=7;
end;

begin
i=10;
a=fib(10);
b:=123456
writeln(b);

aa[3]:=10;
a:=aa[3];
aa[10]:=10;
b:=fib(aa[10]);
writeln(b);
writeln(gao(8));
end.
2 changes: 1 addition & 1 deletion yapc/test/types.pas
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
program types;
vara
var
i : real;
begin
i := 3.2;
Expand Down
63 changes: 56 additions & 7 deletions yapc/yacc_pas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ply import yacc, lex
from AST import Node
from AST import *
from lex_pas import tokens
import lex_pas

from ErrorHandler import SemanticLogger
import logging
Expand Down Expand Up @@ -65,6 +66,11 @@ def p_const_expr(p):
"""
p[0] = Node('const_expr', p.lexer.lineno, p[1], p[3])

#常量定义出错
def p_const_expr_error(p):
'const_expr : error SEMICON'
print("Syntax error in const expression.")


# const_value : INTEGER | REAL | CHAR | STRING | SYS_CON
def p_const_value_1(p):
Expand Down Expand Up @@ -115,6 +121,12 @@ def p_type_definition(p):
p[0] = Node("type_definition", p.lexer.lineno, p[1], p[3])


#type定义出错
def p_type_definition_error(p):
'type_definition : ID EQUAL error SEMICON'
print("Syntax error in type definition.")


def p_type_decl(p):
'''type_decl : simple_type_decl
| array_type_decl
Expand Down Expand Up @@ -161,6 +173,10 @@ def p_record_type_decl(p):
'record_type_decl : kRECORD field_decl_list kEND'
p[0] = Node("record", p.lexer.lineno, p[2])

#record定义出错
def p_record_type_decl_error(p):
'record_type_decl : kRECORD error kEND'
print("Syntax error in record definition.")

def p_field_decl_list(p):
'''field_decl_list : field_decl_list field_decl
Expand All @@ -175,6 +191,10 @@ def p_field_decl(p):
'field_decl : name_list COLON type_decl SEMICON'
p[0] = Node("field_decl", p.lexer.lineno, p[1], p[3])

#record的成员变量定义出错
def p_field_decl_error(p):
'field_decl : error SEMICON'
print("Syntax error in record member definition.")

def p_name_list(p):
'''name_list : name_list COMMA ID
Expand Down Expand Up @@ -207,6 +227,12 @@ def p_var_decl(p):
p[0] = Node("var_decl", p.lexer.lineno, p[1], p[3])


#var定义出错
def p_var_decl_error(p):
'var_decl : error SEMICON'
print("Syntax error in var definition.")


# routine part
def p_routine_part(p):
'''routine_part : routine_part function_decl
Expand All @@ -232,6 +258,13 @@ def p_function_decl(p):
p[0] = Node("function_decl", p.lexer.lineno, p[1], p[3])


#函数体出错
def p_function_decl_error(p):
"""
function_decl : function_head SEMICON error SEMICON
"""
print("Syntax error in function definition.")

def p_function_head(p):
'function_head : kFUNCTION ID parameters COLON simple_type_decl'
p[0] = Node("function_head", p.lexer.lineno, p[2], p[3], p[5])
Expand All @@ -241,6 +274,11 @@ def p_procedure_decl(p):
'procedure_decl : procedure_head SEMICON sub_routine SEMICON'
p[0] = Node("procedure_decl", p.lexer.lineno, p[1], p[3])

#procedure体出错
def p_procedure_decl_error(p):
'procedure_decl : procedure_head SEMICON error SEMICON'
print("Syntax error in procedure definition.")


def p_procedure_head(p):
'procedure_head : kPROCEDURE ID parameters'
Expand Down Expand Up @@ -292,6 +330,11 @@ def p_compound_stmt(p):
'compound_stmt : kBEGIN stmt_list kEND'
p[0] = p[2]

#普通语句出错
def p_compound_stmt_error(p):
'compound_stmt : kBEGIN error kEND'
print("Syntax error in statement. Bad expression")


def p_stmt_list(p):
'''stmt_list : stmt_list stmt SEMICON
Expand All @@ -300,6 +343,12 @@ def p_stmt_list(p):
p[0] = Node("stmt_list", p.lexer.lineno, p[1], p[2])


#statement list出错
def p_stmt_list_error(p):
'stmt_list : stmt_list error SEMICON'
print("Syntax error in statement list.")


def p_stmt(p):
"""
stmt : INTEGER COLON non_label_stmt
Expand Down Expand Up @@ -591,12 +640,12 @@ def p_error(p):
# format="%(filename)10s:%(lineno)4d:%(message)s"
# )
# log = logging.getLogger()
# parser = yacc.yacc(debug=True, debuglog=log)
# test_file = 'test_yacc/simple_arithmetic.pas'
# with open(test_file, 'r') as infile:
# data = infile.read()
# parse_tree_root = parser.parse(data, lexer=lex.lex(module=lex_pas, debug=0), debug=log)
# graph(parse_tree_root, "graph")
#parser = yacc.yacc(debug=True)
#test_file = 'test/test_error.pas'
#with open(test_file, 'r') as infile:
# data = infile.read()
#parse_tree_root = parser.parse(data, lexer=lex.lex(module=lex_pas, debug=0), debug=log)
#graph(parse_tree_root, "graph")
# semantic_logger = SemanticLogger(path.basename(test_file))
# static_semantic_analyzer = SemanticAnalyzer(parse_tree_root, semantic_logger)
# static_semantic_analyzer.analyze()
Expand Down

0 comments on commit 85c34f8

Please sign in to comment.