forked from ruby/lrama
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the codes line numbers in prologue and epilogue
`C_DECLARATION` includes new line after `"%{"` (prologue) or `"%%"` (epilogue) then adjust `#line` directive and source codes in template.
- Loading branch information
Showing
4 changed files
with
126 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
%option noinput nounput noyywrap never-interactive yylineno bison-bridge bison-locations | ||
|
||
%{ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "line_number.h" | ||
|
||
int yycolumn = 0; | ||
|
||
#define YY_USER_ACTION \ | ||
yylloc->first_line = yylloc->last_line = yylineno; \ | ||
yylloc->first_column = yycolumn; \ | ||
yylloc->last_column = yycolumn + yyleng; \ | ||
yycolumn += yyleng; \ | ||
|
||
%} | ||
|
||
NUMBER [0-9]+ | ||
|
||
%% | ||
|
||
{NUMBER} { | ||
yylval->i = atoi(yytext); | ||
return NUM; | ||
} | ||
|
||
[+\-\*\/\(\)] { | ||
return yytext[0]; | ||
} | ||
|
||
[\n|\r\n] {} | ||
|
||
[[:space:]] {} | ||
|
||
<<EOF>> { | ||
return(YYEOF); | ||
} | ||
|
||
. { | ||
fprintf(stderr, "Illegal character '%s'\n", yytext); | ||
return(YYEOF); | ||
} | ||
|
||
%% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
%{ | ||
|
||
#define YYDEBUG 1 | ||
|
||
#include <stdio.h> | ||
#include "line_number.h" | ||
#include "line_number-lexer.h" | ||
|
||
static int yyerror(YYLTYPE *loc, const char *str); | ||
static void line_2(void); | ||
|
||
static void | ||
line_1(void) | ||
{ | ||
printf("line_1: %d\n", __LINE__); | ||
} | ||
|
||
%} | ||
|
||
%union { | ||
int i; | ||
} | ||
|
||
%expect 0 | ||
|
||
%token <i> NUM | ||
|
||
%% | ||
|
||
program : { | ||
printf("line_pre_program: %d\n", __LINE__); | ||
line_1(); | ||
line_2(); | ||
} | ||
expr | ||
{ | ||
printf("line_post_program: %d\n", __LINE__); | ||
} | ||
; | ||
|
||
expr : NUM | ||
; | ||
|
||
%% | ||
|
||
static int | ||
yyerror(YYLTYPE *loc, const char *str) { | ||
fprintf(stderr, "parse error: %s\\n", str); | ||
return 0; | ||
} | ||
|
||
static void | ||
line_2(void) | ||
{ | ||
printf("line_2: %d\n", __LINE__); | ||
} | ||
|
||
int | ||
main(int argc, char *argv[]) { | ||
yydebug = 1; | ||
|
||
if (argc == 2) { | ||
yy_scan_string(argv[1]); | ||
} | ||
|
||
yyparse(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters