Last active
June 7, 2023 09:32
-
-
Save Arathi/f9bfe4413a23ceda8f355d801ed7b959 to your computer and use it in GitHub Desktop.
AHDL语法定义
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
module Adder( | |
i in1, | |
i in2, | |
i carryIn, | |
o out, | |
o carryOut | |
) { | |
wire hout = in1 ^ in2; | |
out = hout ^ carryIn; | |
wire hcar = in1 & in2; | |
wire fcar = hout & carryIn; | |
carryOut = hcar | fcar; | |
} |
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
grammar AHDL; | |
ahdl | |
: (module)* EOF; | |
module | |
: MODULE moduleName LP EOL? portsDeclare RP LC actions RC EOL? | |
; | |
portsDeclare | |
: portDeclare (CM EOL? portDeclare)* | |
; | |
portDeclare | |
: EOL? ioMode ID EOL? | |
| EOL? ioMode bus EOL? | |
; | |
ioMode | |
: inputMode | |
| outputMode | |
; | |
inputMode | |
: I | |
| INPUT | |
; | |
outputMode | |
: O | |
| OUTPUT | |
; | |
bus | |
: ID LB NUMBER RB // busName[index] / busName[amount] | |
| ID LB NUMBER CL NUMBER RB // busName[begin:end] | |
; | |
actions | |
: (EOL? action EOL? SC EOL?)* | |
; | |
action | |
: wireDefine // 线路定义 wire name | |
| submodDefine // 子模块定义 submoduleType name(ports) | |
| assignment // 赋值表达式 | |
; | |
wireDefine | |
: WIRE ID (EQ expression)? | |
; | |
expression | |
: NOT ID | |
| ID AND ID | |
| ID OR ID | |
| ID XOR ID | |
| ID NAND ID | |
| ID XOR ID | |
| ID XNOR ID | |
; | |
submodDefine | |
: moduleType (moduleName)? LP portsAssign RP | |
; | |
moduleType: ID; | |
moduleName: ID; | |
portsAssign | |
: portAssign (CM portAssign)* | |
; | |
portAssign | |
: ID | |
| | |
; | |
assignment | |
: ID EQ expression | |
; | |
/* 运算符 */ | |
// 逻辑运算符 | |
// LOGIC_OPERATORS | |
// : AND | |
// | OR | |
// | XOR | |
// | NOT | |
// | NAND | |
// | NOR | |
// | XNOR | |
// ; | |
// 算术运算符 | |
// ARITHMETIC_OPERATORS | |
// : PLUS | |
// | MINUS | |
// | TIMES | |
// | DIV | |
// ; | |
AND: '&'; | |
OR: '|'; | |
XOR: '^'; | |
NOT: '~'; | |
NAND: NOT AND; | |
NOR: NOT OR; | |
XNOR: NOT XOR; | |
PLUS: '+'; | |
MINUS: '-'; | |
TIMES: '*'; | |
DIV: '/'; | |
CM: ','; // comma | |
DOT: '.'; | |
SC: ';'; // semicolon | |
CL: ':'; // colon | |
AT: '@'; | |
EQ: '='; // equals | |
HA: '#'; | |
/* 定界符 */ | |
LP: '('; | |
RP: ')'; | |
LB: '['; | |
RB: ']'; | |
LC: '{'; | |
RC: '}'; | |
LT: '<'; | |
GT: '>'; | |
/* 关键字 */ | |
MODULE: 'module'; | |
WIRE: 'wire'; | |
BUS: 'bus'; | |
I: 'i'; | |
INPUT: 'input'; | |
O: 'o'; | |
OUTPUT: 'output'; | |
IGNORE: 'ignore'; | |
ID: [_A-Za-z][_0-9A-Za-z]*; | |
NUMBER: [0-9]+; | |
EOL: [\r\n]+; | |
WS: [ \t]+ -> skip; | |
// LINE_COMMENT: '//' -> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment