Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
2
2022fall-Compiler_CMinus
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李晓奇
2022fall-Compiler_CMinus
Commits
38c8231f
Commit
38c8231f
authored
Sep 22, 2022
by
李晓奇
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix the miswritten error from bracket and make the comment rule right!
parent
f3c79837
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
25 additions
and
13 deletions
+25
-13
src/parser/lexical_analyzer.l
src/parser/lexical_analyzer.l
+24
-12
src/parser/syntax_analyzer.y
src/parser/syntax_analyzer.y
+1
-1
tests/parser/syntree_simple/*.syntax_tree
tests/parser/syntree_simple/*.syntax_tree
+0
-0
No files found.
src/parser/lexical_analyzer.l
View file @
38c8231f
...
...
@@ -7,6 +7,8 @@
#include "syntax_tree.h"
#include "syntax_analyzer.h"
/* #define __DEBUG_COMMENT__ */
int lines = 1;
int pos_start;
int pos_end;
...
...
@@ -16,27 +18,32 @@ void pass_node(char *text){
}
void comment_helper(char *comment, unsigned int len)
{
pos_end += 2;
for (int i = 2; i < len-2; i++){
if (comment[i] == '\n' || comment[i] == '\r'){
lines++;
pos_end = 0;
} else
pos_end++;
}
pos_end += 2;
#ifdef __DEBUG_COMMENT__
printf("Get COMMENT in line<%d>: \"%s\"\n", lines, comment);
#endif
/* pos_end += 2;
* for (int i = 2; i < len-2; i++){
* if (comment[i] == '\n' || comment[i] == '\r'){
* lines++;
* pos_end = 0;
* } else
* pos_end++;
* }
* pos_end += 2; */
}
/*****************声明和选项设置 end*****************/
%}
/* use exclusive state */
%x COMMENT
letter [a-zA-Z]
digit [0-9]
ID {letter}+
INTEGER {digit}+
FLOAT {digit}+\.|{digit}*\.{digit}+
COMMENT "/*".*"*/"
NEWLINE \r\n|\r|\n
WHITESPACE [ \t]
...
...
@@ -48,9 +55,14 @@ WHITESPACE [ \t]
%token <node> _SEMI _COMMA _ID _INTEGER _FLOATPOINT
*/
/* <COMMENT>.* { pos_end += yyleng; comment_helper(yytext, yyleng); } */
%%
{COMMENT} { comment_helper(yytext, yyleng); }
<INITIAL>"/*" { BEGIN(COMMENT); pos_end += 2; }
<COMMENT>[^*\n]*|"*"+[^*/\n]* { pos_end += yyleng; comment_helper(yytext, yyleng); }
<COMMENT>"*/" { BEGIN(0); pos_end += 2; }
if { pos_start = pos_end; pos_end += 2; pass_node("if"); return _IF;}
else { pos_start = pos_end; pos_end += 4; pass_node("else"); return _ELSE;}
while { pos_start = pos_end; pos_end += 5; pass_node("while"); return _WHILE;}
...
...
@@ -75,5 +87,5 @@ void { pos_start = pos_end; pos_end += 4; pass_node("void"); return _VOID;}
","|";" { pos_start = pos_end; pos_end += 1; pass_node(yytext); return yytext[0] == ',' ? _COMMA : _SEMI;}
{WHITESPACE} { pos_end++; }
{NEWLINE} { lines++; pos_end = 0;}
<*>
{NEWLINE} { lines++; pos_end = 0;}
%%
src/parser/syntax_analyzer.y
View file @
38c8231f
...
...
@@ -82,7 +82,7 @@ DEC: VAR_DEC {$$ = node("declaration", 1, $1); }
;
VAR_DEC: TYPE_SPEC _ID _SEMI {$$ = node("var-declaration", 3, $1, $2, $3); }
| TYPE_SPEC _ID _L_
BRACKET _INTEGER _R_BRACKET
_SEMI {$$ = node("var-declaration", 6, $1, $2, $3, $4, $5, $6); }
| TYPE_SPEC _ID _L_
SQUARE _INTEGER _R_SQUARE
_SEMI {$$ = node("var-declaration", 6, $1, $2, $3, $4, $5, $6); }
;
TYPE_SPEC: _INT {$$ = node("type-specifier", 1, $1); }
...
...
tests/parser/syntree_simple/*.syntax_tree
0 → 100644
View file @
38c8231f
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment