-
Notifications
You must be signed in to change notification settings - Fork 1
/
frontend.l
118 lines (100 loc) · 2.77 KB
/
frontend.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
%option noyywrap
%option yylineno
/* Currently only support Tofino P4_14 */
%s BMV2_S TOFINO_S
%{
#include <string>
#include <cstdio>
#include "include/ast_nodes.h" // Must be before tab.h
#include "include/ast_nodes_p4.h"
#include "include/ast_nodes_p4r.h"
#include "frontend.tab.h"
%}
%%
%{
// Copied verbatim at the very front of yylex()
extern int with_tofino;
extern int done_init;
if (!done_init)
{
if(with_tofino) {
BEGIN TOFINO_S;
done_init=1; // necessary
return START_TOFINO;
} else {
BEGIN BMV2_S;
done_init=1;
return START_BMV2;
}
}
%}
"#".* {
/* Includes are just copied to output */
// Currently assumes no p4r include
yylval.sval = strdup(yytext);
return INCLUDE;
}
<TOFINO_S>"@".* {
/* For tofino compiler pragma only */
yylval.sval = strdup(yytext);
return PRAGMA;
}
"//".* { /* comments,do nothing */ }
[/][*][^*]*[*]+([^*/][^*]*[*]+)*[/] { /* comments,do nothing */ }
[ \t\r]+ { /* whitespace*/ }
[\n] { /* newline */ }
/* Reserved characters */
"{" {return L_BRACE;}
"}" {return R_BRACE;}
"(" {return L_PAREN;}
")" {return R_PAREN;}
";" {return SEMICOLON;}
":" {return COLON;}
"," {return COMMA;}
"." {return PERIOD;}
"$" {return DOLLAR;}
"[" {return L_BRACKET;}
"]" {return R_BRACKET;}
"/" {return SLASH;}
/* Reserved words - variables */
"malleable" {return P4R_MALLEABLE;}
"table" {return TABLE;}
"value" {return VALUE;}
"field" {return FIELD;}
"width" {return WIDTH;}
"init" {return INIT;}
"alts" {return ALTS;}
"reads" {return READS;}
"actions" {return ACTIONS;}
/* Reserved words - initialization */
"init_block" {return P4R_INIT_BLOCK;}
"register" {return REGISTER;}
/* Reserved words - reactions */
"reaction" {return P4R_REACTION;}
/* Note that tofino blackbox also uses reg as keyword */
"reg" {return REACTION_ARG_REG;}
"ing" {return REACTION_ARG_ING;}
"egr" {return REACTION_ARG_EGR;}
/* Reserved words - P4 */
"header" {return HEADER;}
"metadata" {return METADATA;}
"header_type" {return HEADER_TYPE;}
"fields" {return FIELDS;}
"exact" {return EXACT;}
"ternary" {return TERNARY;}
"action" {return ACTION;}
/* Parsed identifier word in P4 code. */
[A-Za-z_][A-Za-z0-9_]* {
yylval.sval = strdup(yytext);
return IDENTIFIER;
}
/* Integer */
[-]?[0-9]+ {
yylval.sval = strdup(yytext);
return INTEGER;
}
[^{}\/()\[\]:;,\.$ \t\n]+ {
yylval.sval = strdup(yytext);
return STRING;
}
%%