source: tools/prettyprinter/lex.ll

Last change on this file was bd946e4, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

remove newline on C++ comments so it does not appear twice

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3 *
4 * The contents of this file are covered under the licence agreement in the
5 * file "LICENCE" distributed with Cforall.
6 *
7 * lex.ll --
8 *
9 * Author           : Peter A. Buhr
10 * Created On       : Sat Dec 15 11:45:59 2001
11 * Last Modified By : Peter A. Buhr
12 * Last Modified On : Thu May 31 08:49:58 2018
13 * Update Count     : 274
14 */
15
16%option stack
17%option yylineno
18%option nounput
19
20%{
21#include <list>
22#include <string>
23#include <iostream>
24using namespace std;
25#include "ParserTypes.h"
26#include "parser.hh"
27
28#define RETURN_TOKEN( kind ) yylval.tokenp = new Token( yytext, ws_list, kind ); return kind;
29
30list<string> ws_list;
31string comment_str;
32string code_str;
33
34// Stop warning due to incorrectly generated flex code.
35#pragma GCC diagnostic ignored "-Wsign-compare"
36%}
37
38integer [0-9]+
39identifier [a-zA-Z_$][0-9a-zA-Z_$]*
40
41simple_escape ['"?\\]
42escape_sequence [\\]{simple_escape}
43c_char [^'\\\n]|{escape_sequence}
44s_char [^"\\\n]|{escape_sequence}
45
46%x C_COMMENT STR C_CODE
47
48/* ---------------------------- Token Section ---------------------------- */
49%%
50<INITIAL,C_CODE>"/*" {                                                                  // C style comments */
51#if defined(DEBUG_ALL) | defined(DEBUG_COMMENT)
52                cerr << "\"/*\" : " << yytext << endl;
53#endif
54                if ( YYSTATE == C_CODE ) code_str += yytext;
55                else comment_str += yytext;
56                yy_push_state(C_COMMENT);
57}
58<C_COMMENT>(.|"\n")     {                                                                       // C style comments
59#if defined(DEBUG_ALL) | defined(DEBUG_COMMENT)
60                cerr << "<C_COMMENT>(.|\\n) : " << yytext << endl;
61#endif
62                if ( yy_top_state() == C_CODE ) code_str += yytext;
63                else comment_str += yytext;
64}
65<C_COMMENT>"*/" {                                                                               // C style comments
66#if defined(DEBUG_ALL) | defined(DEBUG_COMMENT)
67        cerr << "<C_COMMENT>\"*/\" : " << yytext << endl;
68#endif
69        if ( yy_top_state() == C_CODE ) code_str += yytext;
70        else {
71                comment_str += yytext;
72                //cerr << "C COMMENT : " << endl << comment_str << endl;
73                ws_list.push_back( comment_str );
74                comment_str = "";
75        }
76        yy_pop_state();
77}
78
79<INITIAL,C_CODE>"//"[^\n]* {                                                    // C++ style comments
80#if defined(DEBUG_ALL) | defined(DEBUG_COMMENT)
81        cerr << "\"//\"[^\\n]*\"\n\" : " << yytext << endl;
82#endif
83        if ( YYSTATE == C_CODE ) code_str += yytext;
84        else {
85                comment_str += yytext;
86                //cerr << "C++ COMMENT : " << endl << comment_str << endl;
87                ws_list.push_back( comment_str );
88                comment_str = "";
89        }
90}
91
92";"                             { RETURN_TOKEN( ';' ) }
93":"                             { RETURN_TOKEN( ':' ) }
94"|"                             { RETURN_TOKEN( '|' ) }
95","                             { RETURN_TOKEN( ',' ) }
96"<"                             { RETURN_TOKEN( '<' ) }
97">"                             { RETURN_TOKEN( '>' ) }
98
99[[:space:]]+ {                                                                                  // [ \t\n]+
100        ws_list.push_back( yytext );
101        //cerr << "WS : " << "\"" << yytext << "\"" << endl;
102}
103
104<INITIAL>"{"    { RETURN_TOKEN( '{' ) }
105<INITIAL>"}"    { RETURN_TOKEN( '}' ) }
106<C_CODE>"{"     {
107#if defined(DEBUG_ALL) | defined(DEBUG_C)
108        cerr << "<C_CODE>. : " << yytext << endl;
109#endif
110        code_str += yytext;
111        RETURN_TOKEN( '{' )
112}
113<C_CODE>"}"     {
114#if defined(DEBUG_ALL) | defined(DEBUG_C)
115        cerr << "<C_CODE>. : " << yytext << endl;
116#endif
117        code_str += yytext;
118        RETURN_TOKEN( '}' )
119}
120
121"%%"                    { RETURN_TOKEN( MARK ) }
122"%{"                    { RETURN_TOKEN( LCURL ) }
123<C_CODE>"%}"    { RETURN_TOKEN( RCURL ) }
124
125^"%define"[^\n]*"\n" { RETURN_TOKEN( DEFINE ) }
126^"%expect"              { RETURN_TOKEN( EXPECT ) }
127^"%left"                { RETURN_TOKEN( LEFT ) }
128^"%locations"   { RETURN_TOKEN( LOCATIONS ) }
129^"%nonassoc"    { RETURN_TOKEN( NONASSOC ) }
130^"%precedence"  { RETURN_TOKEN( PRECEDENCE ) }
131^"%pure_parser" { RETURN_TOKEN( PURE_PARSER ) }
132^"%right"               { RETURN_TOKEN( RIGHT ) }
133^"%semantic_parser"     { RETURN_TOKEN( SEMANTIC_PARSER ) }
134^"%start"               { RETURN_TOKEN( START ) }
135^"%thong"               { RETURN_TOKEN( THONG ) }
136^"%token"               { RETURN_TOKEN( TOKEN ) }
137^"%type"                { RETURN_TOKEN( TYPE ) }
138^"%union"               { RETURN_TOKEN( UNION ) }
139
140"%prec"                 { RETURN_TOKEN( PREC ) }
141
142{integer}               { RETURN_TOKEN( INTEGER ); }
143[']{c_char}[']  { RETURN_TOKEN( CHARACTER ); }
144{identifier}    { RETURN_TOKEN( IDENTIFIER ); }
145
146<C_CODE>["]{s_char}*["] {                                                               // hide braces "{}" in strings
147#if defined(DEBUG_ALL) | defined(DEBUG_C)
148        cerr << "<C_CODE>. : " << yytext << endl;
149#endif
150        code_str += yytext;
151}
152
153<C_CODE>(.|\n) {                                                                                // must be last rule of C_CODE
154#if defined(DEBUG_ALL) | defined(DEBUG_C)
155        cerr << "<C_CODE>. : " << yytext << endl;
156#endif
157        code_str += yytext;
158}
159
160                                /* unknown characters */
161.                               { printf("unknown character(s):\"%s\" on line %d\n", yytext, yylineno); }
162%%
163void lexC(void) {
164        BEGIN(C_CODE);
165}
166
167string lexYacc(void) {
168        BEGIN(INITIAL);
169        //cerr << "CODE: " << endl << code_str << endl;
170        string temp( code_str );
171        code_str = "";
172        return temp;
173}
174
175// Local Variables: //
176// mode: c++ //
177// tab-width: 4 //
178// compile-command: "make install" //
179// End: //
Note: See TracBrowser for help on using the repository browser.