1 %{ 2 /**************************************************************************** 3 Copyright (C) 1987-2015 by Jeffery P. Hansen 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License along 16 with this program; if not, write to the Free Software Foundation, Inc., 17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 ****************************************************************************/ 19 #include <unistd.h> 20 #include <string.h> 21 #include <ctype.h> 22 #include "config.h" 23 #include "yybasic.h" 24 #include "ycmalloc.h" 25 #include "vgrammar.h" 26 27 #ifndef YY_START 28 #define YY_START YYSTATE 29 #endif 30 31 /***************************************************************************** 32 * 33 * Flex has some errors that cause its output to generate several spurious 34 * warnings when compiling using gcc and the -Wall option. One of the errors 35 * is a goto label 'find_rule' that is never used. If SUPPRESS_FLEX_ERRORS 36 * is non-zero, then we will try to suppress the error by inserting a goto 37 * that label that is conditional on a zero condition so that this in fact 38 * acts as a noop. 39 * 40 *****************************************************************************/ 41 #if SUPPRESS_FLEX_ERRORS 42 #define FLEX_FIX_STAT if (0) goto find_rule; 43 #else 44 #define FLEX_FIX_STAT 45 #endif 46 47 48 /* 49 * This flag should be set to zero if we are running as the simulator 50 */ 51 int yy_is_editor = 1; 52 53 struct lex_keywordentry lex_verilog_words[] = { 54 {"always",ALWAYS}, 55 {"assign",ASSIGN}, 56 {"begin",BBEGIN}, 57 {"case",CASE}, 58 {"casex",CASEX}, 59 {"casez",CASEZ}, 60 {"deassign",DEASSIGN}, 61 {"default",DEFAULT}, 62 {"else",ELSE}, 63 {"end",END}, 64 {"endcase",ENDCASE}, 65 {"endmodule",ENDMODULE}, 66 {"endprimitive",ENDPRIMITIVE}, 67 {"if",IF}, 68 {"initial",INITIALB}, 69 {"inout",INOUT}, 70 {"input",INPUT}, 71 {"integer",INTEGER}, 72 {"module",XMODULE}, 73 {"negedge",NEGEDGE}, 74 {"output",OUTPUT}, 75 {"posedge",POSEDGE}, 76 {"primitive",PRIMITIVE}, 77 {"reg",REG}, 78 {"supply0",SUPPLY0}, 79 {"supply1",SUPPLY1}, 80 {"tran",TRAN}, 81 {"tri",KWTRI}, 82 {"tri0",TRI0}, 83 {"tri1",TRI1}, 84 {"triand",TRIAND}, 85 {"trior",TRIOR}, 86 {"trireg",TRIREG}, 87 {"wand",WAND}, 88 {"wire",WIRE}, 89 {"wor",WOR}, 90 }; 91 int lex_verilog_num = sizeof(lex_verilog_words)/sizeof(lex_verilog_words[0]); 92 93 struct lex_keywordentry lex_anotate_words[] = { 94 {"beginscript",BEGINSCRIPT}, 95 {"comment",COMMENT}, 96 {"enddecls",ENDDECLS}, 97 {"endscript",ENDSCRIPT}, 98 {"frame",FRAME}, 99 {"interface",BDESC}, 100 {"joint",JOINT}, 101 {"property",PROPERTY}, 102 {"require",REQUIRE}, 103 {"root_module",ROOTMODULE}, 104 {"script",SCRIPT}, 105 {"version",KWVERSION}, 106 }; 107 int lex_anotate_num = sizeof(lex_anotate_words)/sizeof(lex_anotate_words[0]); 108 109 struct lex_keywordentry lex_aoption_words[] = { 110 {"/bd",BDPORTS}, 111 {"/builtinBegin",BUILTINBEGIN}, 112 {"/builtinEnd",END}, 113 {"/data",DATA}, 114 {"/dp",DECPOS}, 115 {"/end",COMEND}, 116 {"/hdlBegin",HDLBEGIN}, 117 {"/hdlEnd",END}, 118 {"/iconBegin",ICONDATA}, 119 {"/iconEnd",ICONEND}, 120 {"/netlistBegin",NETLISTBEGIN}, 121 {"/netlistEnd",END}, 122 {"/p",PORTS}, 123 {"/port",PORTDEF}, 124 {"/r",ROT}, 125 {"/sn",SHOWNAME}, 126 {"/symbol",SYMBOLREF}, 127 {"/symbolBegin",SYMBOLBEGIN}, 128 {"/symbolEnd",END}, 129 {"/sz",SIZE}, 130 {"/textBegin",TEXTBEGIN}, 131 {"/textEnd",END}, 132 {"/w",WPLACE}, 133 }; 134 int lex_aoption_num = sizeof(lex_aoption_words)/sizeof(lex_aoption_words[0]); 135 136 137 struct lex_keywordentry lex_delay_words[] = { 138 {"area",AREA}, 139 {"break",BREAK}, 140 {"case",CASE}, 141 {"default",DEFAULT}, 142 {"delay",DELAY}, 143 {"else",ELSE}, 144 {"if",IF}, 145 {"power",POWER}, 146 {"primitive",PRIMITIVE}, 147 {"return",RETURN}, 148 {"switch",SWITCH}, 149 {"technology",TECHNOLOGY}, 150 {"version",KWVERSION}, 151 }; 152 int lex_delay_num = sizeof(lex_delay_words)/sizeof(lex_delay_words[0]); 153 154 static int yc_last_mode; 155 156 int ycLiteral(char *Tok,struct lex_keywordentry *low,int len); 157 int ycString(char *S); 158 int ycCheckHdlLine(char *S); 159 160 int ycLineNumber; 161 const char *ycFileName; 162 163 /* 164 Start states: 165 PV Pure Verilog 166 VR TkGate editor Verilog 167 BC Block comment 168 LC Line comment 169 AC Annotation comment 170 DD Delay definition 171 DDP Delay definition parameter 172 */ 173 %} 174 175 %start VR BC LC AC DD DDP VN HDL PV 176 177 white [ \t\r] 178 num [0-9] 179 hex [0-9A-Fa-f] 180 lit1 [A-Za-z_] 181 lit2 [A-Za-z0-9_] 182 litddp [A-Za-z0-9_/*-] 183 vnset [A-Za-z0-9.] 184 any [^ \t\r\n] 185 186 %% 187 188 <PV,VR,DD,DDP>"/*" { SaveLast(); BeginBC(); } 189 <BC>"*/" { BeginLast(); } 190 <BC>. { } 191 <BC>\n { ycLineNumber++; } 192 <PV,VR,DD,DDP>"//" { SaveLast(); BeginLC(); } 193 <LC>\n { ycLineNumber++; BeginLast(); } 194 <LC>. { } 195 196 <VR,HDL>"//:" { SaveLast(); BeginAC(); return ANOTATE; } 197 <AC>\n { ycLineNumber++; BeginLast(); return ENDANOTATE; } 198 199 <PV,VR,AC,DD>";" { return SEMI; } 200 <PV,VR,AC,DD>":" { return COLON; } 201 <PV,VR,AC,DD>"," { return COMMA; } 202 <PV,VR,AC,DD>"." { return DOT; } 203 <PV,VR,AC,DD>"=" { return ASGN; } 204 <PV,VR,AC>"<=" { return NBASGN; } 205 <PV,VR,AC>"/" { return SLASH; } 206 <PV,VR,AC,DD>"(" { return LPAREN; } 207 <PV,VR,AC,DD>")" { return RPAREN; } 208 <PV,VR,AC,DD>"{" { return LBRACE; } 209 <PV,VR,AC,DD>"}" { return RBRACE; } 210 <PV,VR,AC,DD>"[" { return LBRACK; } 211 <PV,VR,AC,DD>"]" { return RBRACK; } 212 <PV,VR,AC,DD>"!" { return NOT; } 213 <PV,VR,AC,DD>"@" { return AT; } 214 <PV,VR,AC,DD>"#" { return HASHMARK; } 215 <PV,VR,AC,DD,DDP>">" { return GT; } 216 <PV,VR,AC,DD,DDP>"<" { return LT; } 217 218 <DD>"**" { return POW; } 219 <PV,VR,DD>"||" { return OR; } 220 <PV,VR,DD>"&&" { return AND; } 221 <PV,VR,DD>">=" { return GE; } 222 <PV,VR,DD>"<=" { return LE; } 223 <PV,VR,DD>"==" { return EQ; } 224 <PV,VR,DD>"!=" { return NE; } 225 <PV,VR,DD>"+" { return ADD; } 226 <PV,VR,DD>"-" { return SUB; } 227 <PV,VR,DD>"*" { return MUL; } 228 <PV,VR,DD>"/" { return DIV; } 229 <PV,VR,DD>"%" { return MOD; } 230 <PV,VR,DD>"~&" { return BNAND; } 231 <PV,VR,DD>"~|" { return BNOR; } 232 <PV,VR,DD>"&" { return BAND; } 233 <PV,VR,DD>"|" { return BOR; } 234 <PV,VR,DD>"^" { return BXOR; } 235 <PV,VR,DD>"~^" { return BNXOR; } 236 <PV,VR,DD>"~" { return UINV; } 237 <PV,VR,DD>">>" { return RSHIFT; } 238 <PV,VR,DD>"<<" { return LSHIFT; } 239 <PV,VR,DD>"?" { return QUEST; } 240 <PV>">>>" { return ARSHIFT; } 241 <PV>"<<<" { return ALSHIFT; } 242 243 <PV,VR,AC,DD>\"([^\\\"]*("\\\\"|"\\\"")*)*\" { return ycString(yytext); } 244 245 <PV,VR,AC,DD>{num}+"'h"{hex}+ { yylval.S = yc_strdup(yytext); return HEX; } 246 <PV,VR,AC,DD>{num}+"'b"{hex}+ { yylval.S = yc_strdup(yytext); return HEX; } 247 <PV,VR,AC,DD>{num}+"'o"{hex}+ { yylval.S = yc_strdup(yytext); return HEX; } 248 <PV,VR,AC,DD>{num}+"'d"{hex}+ { yylval.S = yc_strdup(yytext); return HEX; } 249 250 <DD>{num}+ { sscanf(yytext,"%d",&yylval.I); return NUMBER; } 251 <PV,VR,AC>"-"?{num}+ { sscanf(yytext,"%d",&yylval.I); return NUMBER; } 252 <DDP>{litddp}+ { return ycLiteral(yytext,lex_delay_words,lex_delay_num); } 253 <DD>{lit1}{lit2}* { return ycLiteral(yytext,lex_delay_words,lex_delay_num); } 254 255 <PV,VR>\\{any}+ { yylval.S = yc_strdup(yytext+1); return LITERAL; } 256 <PV,VR>{lit1}{lit2}* { return ycLiteral(yytext,lex_verilog_words,lex_verilog_num); } 257 <AC>{lit1}{lit2}* { return ycLiteral(yytext,lex_anotate_words,lex_anotate_num); } 258 <AC>"/"{lit1}{lit2}* { return ycLiteral(yytext,lex_aoption_words,lex_aoption_num); } 259 <PV>${lit1}{lit2}* { yylval.S = yc_strdup(yytext); return SYSLITERAL; } 260 <PV>. { return BOGOCHAR; } 261 262 <VN>{vnset}+ { yylval.S = yc_strdup(yytext); return VERNUM; } 263 <PV,VR,AC,DD,DDP,VN>{white}+ { } 264 <PV,VR,DD,DDP,VN>"\n" { ycLineNumber++; } 265 266 <VR>"`"{lit1}{lit2}*.*"\n" { ycDirective(yytext); } 267 268 <HDL>.*"\n" { ycLineNumber++; return ycCheckHdlLine(yytext); } 269 270 . { FLEX_FIX_STAT yyerror("Illegal character (%d) '%c'.\n",*yytext,*yytext); return BOGOCHAR; } 271 272 273 %% 274 275 void BeginPV() { BEGIN PV; } 276 void BeginVR() { BEGIN VR; } 277 void BeginAC() { BEGIN AC; } 278 void BeginLC() { BEGIN LC; } 279 void BeginBC() { BEGIN BC; } 280 void BeginDD() { BEGIN DD; } 281 void BeginDDP() { BEGIN DDP; } 282 void BeginVN() { BEGIN VN; } 283 void BeginHDL() { if (yy_is_editor) { BEGIN HDL; } } 284 void BeginLast() { BEGIN yc_last_mode; } 285 void SaveLast() { yc_last_mode = YY_START; } 286 287 /***************************************************************************** 288 * 289 * If flex error suppression is on, make some calls to these functions that 290 * are defined but not used by flex to suppress spurious warnings. 291 * 292 *****************************************************************************/ 293 #if SUPPRESS_FLEX_ERRORS 294 void yylexjunk() 295 { 296 yy_flex_realloc(0,0); 297 yyunput(0,0); 298 yy_flex_strlen(0); 299 } 300 #endif 301