Skip to content
Snippets Groups Projects
Unverified Commit c3dd86c0 authored by chaoei's avatar chaoei Committed by GitHub
Browse files

Merge pull request #1 from ppx123-web/Lab1

  Sat Mar 12 05:11:34 PM CST 2022
parents 98d9eb35 1275db95
No related branches found
No related tags found
No related merge requests found
......@@ -6,4 +6,4 @@ Project*.pdf
*/*.output
!*/CMakeLists.txt
*/*.txt
*/cmake-build-debug/
cmake-build-debug/
File deleted
cmake_minimum_required(VERSION 3.21)
project(Lab_compiler C)
set(CMAKE_C_STANDARD 99)
include_directories(${PROJECT_SOURCE_DIR}/Code)
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
set(CMAKE_C_FLAGS "-lfl -ly")
elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
set(CMAKE_C_FLAGS "-LC:/GnuWin32/lib/")
endif(CMAKE_SYSTEM_NAME MATCHES "Linux")
file(GLOB LEX_FILE ${PROJECT_SOURCE_DIR}/Code/*.l)
add_custom_target(TEMP)
add_custom_command(TARGET TEMP
PRE_BUILD
COMMAND flex -o ${PROJECT_SOURCE_DIR}/Code/lex.yy.c ${PROJECT_SOURCE_DIR}/Code/lexical.l
COMMAND bison -o ${PROJECT_SOURCE_DIR}/Code/syntax.tab.c -d -v ${PROJECT_SOURCE_DIR}/Code/syntax.y
)
file(GLOB SRC_LIST ${PROJECT_SOURCE_DIR}/Code/*.c)
file(GLOB UNUSED ${PROJECT_SOURCE_DIR}/Code/lex.yy.c)
list(REMOVE_ITEM SRC_LIST ${UNUSED})
add_executable(parse ${SRC_LIST})
message("files:${SRC_LIST}")
message("Exclude:${UNUSED}")
../Test/test0.cmm:
Program (1)
ExtDefList (1)
ExtDef (1)
......@@ -34,6 +38,10 @@ Program (1)
INT: 2
SEMI
RC
../Test/test1.cmm:
Program (1)
ExtDefList (1)
ExtDef (1)
......@@ -70,8 +78,14 @@ Program (1)
INT: 63
SEMI
RC
Error type B at Line 3: Syntax error. '9'
Error type B at Line 4: Syntax error. 'G'
../Test/test2.cmm:
../Test/test3.cmm:
Program (1)
ExtDefList (1)
ExtDef (1)
......@@ -96,7 +110,14 @@ Program (1)
FLOAT: 0.000105
SEMI
RC
Error type B at Line 3: Syntax error. 'e'
../Test/test4.cmm:
../Test/test5.cmm:
Program (1)
ExtDefList (1)
ExtDef (1)
......
#line 1 "./lex.yy.c"
#line 1 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lex.yy.c"
#line 3 "./lex.yy.c"
#line 3 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lex.yy.c"
#define YY_INT_ALIGNED short int
......@@ -527,8 +527,8 @@ int yy_flex_debug = 0;
#define YY_MORE_ADJ 0
#define YY_RESTORE_YY_MORE_OFFSET
char *yytext;
#line 1 "./lexical.l"
#line 2 "./lexical.l"
#line 1 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
#line 2 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
#include <stdio.h>
#include "debug.h"
#include "syntax.tab.h"
......@@ -538,8 +538,8 @@ char *yytext;
int Iscomments = 0;
extern int syntax;
static Node_t* add_node_text(char * content,char *text,int len);
#line 541 "./lex.yy.c"
#line 542 "./lex.yy.c"
#line 541 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lex.yy.c"
#line 542 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lex.yy.c"
#define INITIAL 0
......@@ -756,9 +756,9 @@ YY_DECL
}
{
#line 43 "./lexical.l"
#line 43 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
#line 761 "./lex.yy.c"
#line 761 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lex.yy.c"
while ( /*CONSTCOND*/1 ) /* loops until end-of-file is reached */
{
......@@ -817,159 +817,159 @@ do_action: /* This label is used only to access EOF actions. */
case 1:
YY_RULE_SETUP
#line 44 "./lexical.l"
#line 44 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("STRUCT"); return STRUCT; }
YY_BREAK
case 2:
YY_RULE_SETUP
#line 45 "./lexical.l"
#line 45 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("RETURN"); return RETURN; }
YY_BREAK
case 3:
YY_RULE_SETUP
#line 46 "./lexical.l"
#line 46 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("IF"); return IF; }
YY_BREAK
case 4:
YY_RULE_SETUP
#line 47 "./lexical.l"
#line 47 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("ELSE"); return ELSE; }
YY_BREAK
case 5:
YY_RULE_SETUP
#line 48 "./lexical.l"
#line 48 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("WHILE"); return WHILE; }
YY_BREAK
case 6:
YY_RULE_SETUP
#line 49 "./lexical.l"
#line 49 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("TYPE"); return TYPE; }
YY_BREAK
case 7:
/* rule 7 can match eol */
YY_RULE_SETUP
#line 50 "./lexical.l"
#line 50 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ unput(yytext[yyleng-1]); yycolumn--,yytext[yyleng-1] = 0;ELEMENT("INT"); return INT;}
YY_BREAK
case 8:
YY_RULE_SETUP
#line 51 "./lexical.l"
#line 51 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("FLOAT"); return FLOAT;}
YY_BREAK
case 9:
YY_RULE_SETUP
#line 52 "./lexical.l"
#line 52 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("ID"); return ID; }
YY_BREAK
case 10:
YY_RULE_SETUP
#line 53 "./lexical.l"
#line 53 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("SEMI"); return SEMI;}
YY_BREAK
case 11:
YY_RULE_SETUP
#line 54 "./lexical.l"
#line 54 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("COMMA"); return COMMA;}
YY_BREAK
case 12:
YY_RULE_SETUP
#line 55 "./lexical.l"
#line 55 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("ASSIGNOP"); return ASSIGNOP;}
YY_BREAK
case 13:
YY_RULE_SETUP
#line 56 "./lexical.l"
#line 56 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("PLUS"); return PLUS;}
YY_BREAK
case 14:
YY_RULE_SETUP
#line 57 "./lexical.l"
#line 57 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("MINUS"); return MINUS;}
YY_BREAK
case 15:
YY_RULE_SETUP
#line 58 "./lexical.l"
#line 58 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("STAR"); return STAR;}
YY_BREAK
case 16:
YY_RULE_SETUP
#line 59 "./lexical.l"
#line 59 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("DIV"); return DIV;}
YY_BREAK
case 17:
YY_RULE_SETUP
#line 60 "./lexical.l"
#line 60 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("AND"); return AND;}
YY_BREAK
case 18:
YY_RULE_SETUP
#line 61 "./lexical.l"
#line 61 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("OR"); return OR;}
YY_BREAK
case 19:
YY_RULE_SETUP
#line 62 "./lexical.l"
#line 62 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("DOT"); return DOT;}
YY_BREAK
case 20:
YY_RULE_SETUP
#line 63 "./lexical.l"
#line 63 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("NOT"); return NOT;}
YY_BREAK
case 21:
YY_RULE_SETUP
#line 64 "./lexical.l"
#line 64 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("LP"); return LP;}
YY_BREAK
case 22:
YY_RULE_SETUP
#line 65 "./lexical.l"
#line 65 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("RP"); return RP;}
YY_BREAK
case 23:
YY_RULE_SETUP
#line 66 "./lexical.l"
#line 66 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("LB"); return LB;}
YY_BREAK
case 24:
YY_RULE_SETUP
#line 67 "./lexical.l"
#line 67 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("RB"); return RB;}
YY_BREAK
case 25:
YY_RULE_SETUP
#line 68 "./lexical.l"
#line 68 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("LC"); return LC;}
YY_BREAK
case 26:
YY_RULE_SETUP
#line 69 "./lexical.l"
#line 69 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ ELEMENT("RC"); return RC;}
YY_BREAK
case 27:
YY_RULE_SETUP
#line 70 "./lexical.l"
#line 70 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ strcat(linetext," ");}
YY_BREAK
case 28:
YY_RULE_SETUP
#line 71 "./lexical.l"
#line 71 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ yycolumn += 3; strcat(linetext,"\t");}
YY_BREAK
case 29:
/* rule 29 can match eol */
YY_RULE_SETUP
#line 72 "./lexical.l"
#line 72 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ yycolumn = 1; yylineno += 1; memset(linetext,0,sizeof(linetext)); }
YY_BREAK
case 30:
YY_RULE_SETUP
#line 73 "./lexical.l"
#line 73 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ char c = input(); while(c != '\n') { c = input(); } yylineno += 1; }
YY_BREAK
case 31:
YY_RULE_SETUP
#line 74 "./lexical.l"
#line 74 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{
char c1 = input(),c2 = input();
if (c1 == '\n') {
......@@ -992,15 +992,15 @@ YY_RULE_SETUP
YY_BREAK
case 32:
YY_RULE_SETUP
#line 93 "./lexical.l"
#line 93 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
{ LexicalError("%s",yytext); syntax++; }
YY_BREAK
case 33:
YY_RULE_SETUP
#line 95 "./lexical.l"
#line 95 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
ECHO;
YY_BREAK
#line 1003 "./lex.yy.c"
#line 1003 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lex.yy.c"
case YY_STATE_EOF(INITIAL):
yyterminate();
......@@ -2005,7 +2005,7 @@ void yyfree (void * ptr )
#define YYTABLES_NAME "yytables"
#line 95 "./lexical.l"
#line 95 "/home/ppx/zhaochaoyi/Lab-compiler/Code/lexical.l"
extern int syntax;
......@@ -2015,7 +2015,7 @@ void yyerror(char* msg) {
if(strcmp(msg,"syntax error") == 0) {
SyntaxError("%s : %s\n\t%s\n",msg,yytext,linetext);
} else {
//SyntaxError("(from syntax.y): %s\n",msg);
SyntaxError("(from syntax.y): %s\n",msg);
}
}
......@@ -2028,16 +2028,3 @@ static Node_t* add_node_text(char * content,char *text,int len) {
// #ifdef SCANNER_DEBUG
// int main(int argc,char* argv[]) {
// if(argc > 1) {
// if(!(yyin = fopen(argv[1],"r"))) {
// perror(argv[1]);
// return 1;
// }
// }
// while (yylex() != 0);
// return 0;
// }
// #endif
......@@ -101,7 +101,7 @@ void yyerror(char* msg) {
if(strcmp(msg,"syntax error") == 0) {
SyntaxError("%s : %s\n\t%s\n",msg,yytext,linetext);
} else {
//SyntaxError("(from syntax.y): %s\n",msg);
SyntaxError("(from syntax.y): %s\n",msg);
}
}
......@@ -113,16 +113,3 @@ static Node_t* add_node_text(char * content,char *text,int len) {
}
// #ifdef SCANNER_DEBUG
// int main(int argc,char* argv[]) {
// if(argc > 1) {
// if(!(yyin = fopen(argv[1],"r"))) {
// perror(argv[1]);
// return 1;
// }
// }
// while (yylex() != 0);
// return 0;
// }
// #endif
......@@ -15,12 +15,10 @@ int main(int argc,char *argv[]) {
printf("Usage:%s $FILE\n",argv[0]);
}
for (int i = 1;i < argc;i++) {
#ifndef FINAL
fprintf(stderr,"\n\n\n");
fprintf(stderr,"%s:\n",argv[i]);
printf("\n\n\n");
printf("%s:\n",argv[i]);
#endif
#ifndef FINAL
printf("\n\n\n");
printf("%s:\n",argv[i]);
#endif
syntax = 0;
yycolumn = 1;
yylineno = 1;
......
File deleted
This diff is collapsed.
......@@ -35,8 +35,8 @@
especially those whose name start with YY_ or yy_. They are
private implementation details that can be changed or removed. */
#ifndef YY_YY_SYNTAX_TAB_H_INCLUDED
# define YY_YY_SYNTAX_TAB_H_INCLUDED
#ifndef YY_YY_HOME_PPX_ZHAOCHAOYI_LAB_COMPILER_CODE_SYNTAX_TAB_H_INCLUDED
# define YY_YY_HOME_PPX_ZHAOCHAOYI_LAB_COMPILER_CODE_SYNTAX_TAB_H_INCLUDED
/* Debug traces. */
#ifndef YYDEBUG
# define YYDEBUG 0
......@@ -90,11 +90,11 @@ extern int yydebug;
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
union YYSTYPE
{
#line 31 "./syntax.y"
#line 31 "/home/ppx/zhaochaoyi/Lab-compiler/Code/syntax.y"
struct __Tree_node_t * node;
#line 98 "./syntax.tab.h"
#line 98 "/home/ppx/zhaochaoyi/Lab-compiler/Code/syntax.tab.h"
};
typedef union YYSTYPE YYSTYPE;
......@@ -123,4 +123,4 @@ extern YYLTYPE yylloc;
int yyparse (void);
#endif /* !YY_YY_SYNTAX_TAB_H_INCLUDED */
#endif /* !YY_YY_HOME_PPX_ZHAOCHAOYI_LAB_COMPILER_CODE_SYNTAX_TAB_H_INCLUDED */
File deleted
# Project 1
错误恢复
Stmt -> error SEMI
Exp -> error RP | error RB | error OPs(+-*/ && || RELOP)
Args -> error COMMA
还需要做:
错误恢复,注释
fun(int a int a) 需要ID LP error RP
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment