xasm.g4 2.23 KB
Newer Older
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

grammar xasm;

/* This part of the grammar is particular to XACC */
/**********************************************************************/
xaccsrc
   : xacckernel
   | xacclambda
   ;

    xacckernel : '__qpu__' 'void' kernelname =
                    id '(' 'qbit' acceleratorbuffer =
                        id (',' typedparam) * ')' '{' mainprog '}';

    xacclambda : '['('&'|'=')?']' '(' 'qbit' acceleratorbuffer=id (',' typedparam) * ')' '{' mainprog '}';

    typedparam : type id;

    type : 'int' | 'double' | 'float' | 'std::vector<double>';

/***********************************************************************/


/* The main program */
mainprog
   : program
   ;

/* The actual program statements */
program
   : (line)+
   ;

line
   : statement+
   | comment
   ;

/* A program statement */
statement
   : instruction ';'
   | 'return' ';'
   ;

/* A program comment */
comment
   : COMMENT
   ;

instruction
Mccaskey, Alex's avatar
Mccaskey, Alex committed
51
   : inst_name=id  '(' (bits_and_params=explist) (',' options=optionsMap)? ')'
52
53
   ;

Mccaskey, Alex's avatar
Mccaskey, Alex committed
54

55
bufferIndex
Mccaskey, Alex's avatar
Mccaskey, Alex committed
56
   : id ('[' INT ']')
57
58
   ;

Mccaskey, Alex's avatar
Mccaskey, Alex committed
59
bitsOrParamType
60
   : bufferIndex (',' bufferIndex)*
Mccaskey, Alex's avatar
Mccaskey, Alex committed
61
   | exp*
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
   ;

optionsMap
   : '{' optionsType (',' optionsType)* '}'
   ;

optionsType
   : '{' key=string ',' value=exp '}'
   ;

/* A list of expressions */
explist
   : exp ( ',' exp )*
   ;

/* An expression */
exp
Mccaskey, Alex's avatar
Mccaskey, Alex committed
79
   : id
80
81
82
83
84
85
86
87
88
   | exp '+' exp
   | exp '-' exp
   | exp '*' exp
   | exp '/' exp
   | '-'exp
   | exp '^' exp
   | '(' exp ')'
   | unaryop '(' exp ')'
   | string
Mccaskey, Alex's avatar
Mccaskey, Alex committed
89
90
91
92
   | real
   | INT
   | 'pi'
   | bufferIndex
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
   ;

/* unary operations */
unaryop
   : 'sin'
   | 'cos'
   | 'tan'
   | 'exp'
   | 'ln'
   | 'sqrt'
   ;

/* variable identity */
id
   : ID
   ;

/* real numbers */
real
   : REAL
   ;

/* strings are enclosed in quotes */
string
   : STRING
   ;

/* Tokens for the grammer */

/* Comment */
COMMENT
   : '#' ~ [\r\n]* EOL
   ;

/* id, ego, and super-ego */
ID
   : [a-z][A-Za-z0-9_]*
   | [A-Z][A-Za-z0-9_]*
   | [A-Z][A-Za-z]*
   ;

/* Keep it real...numbers */
REAL
   : INT ( '.' (INT) )
   ;

/* Non-negative integers */
INT
   : ('0'..'9')+
   ;

/* Strings include numbers and slashes */
STRING
   : '"' ~ ["]* '"'
   ;

/* Whitespaces, we skip'em */
WS
   : [ \t\r\n] -> skip
   ;

/* This is the end of the line, boys */
EOL
: '\r'? '\n'
;