antlr / grammars-v4

Grammars written for ANTLR v4; expectation that the grammars are free of actions.
MIT License
10.09k stars 3.69k forks source link

System Verilog 2017 grammar #1438

Open Nic30 opened 5 years ago

Nic30 commented 5 years ago

Hello,

I am looking for up-to-date complete System Verilog grammar.

I know about:

Do you know about any other System Verilog grammars?

I would like to have 2017 grammar which works exactly as standard describes, but it quite lot of time to extract it and fix it in order to make it work with ANTL4 as the grammar is very large and complex.

zhuzhzh commented 4 years ago

I tried to use the grammer files from them. but nono of them can success with the target Go language. It passes with Python

Nic30 commented 4 years ago

Hello @zhuzhzh,

if you use https://github.com/Nic30/hdlConvertor/blob/master/grammars/sv2017Parser.g4 and https://github.com/Nic30/hdlConvertor/blob/master/grammars/sv2017Lexer.g4

you have to replace c++ in "{}" blocks (semantic predicates) in sv2017Lexer.g4. It should be trivial.

However I guess you also need a preprocessor. https://github.com/Nic30/hdlConvertor/blob/master/grammars/verilogPreprocParser.g4 https://github.com/Nic30/hdlConvertor/blob/master/grammars/verilogPreprocLexer.g4

verilogPreprocLexer.g4 also contains C++ which have to be translated to Go.

Currently I do not know how write grammar which does support multiple target languages (and uses semantic predicates in target language). And it is required to use target language directly sometimes because otherwise some things would be impossible, some extremely slow.

(I would gladly make another targets for grammars mentioned, I just do not know how to do it without code replication.)

KvanTTT commented 4 years ago

Currently I do not know how write grammar which does support multiple target languages (and uses semantic predicates in target language).

Take a took at the approach that used for javascript and php grammars. It works at least for C#, Java, and can be adapted on other runtimes.

Nic30 commented 4 years ago

@KvanTTT , nice I do not know how could I miss this, I was looking at grammars-v4.

Nic30 commented 4 years ago

@KvanTTT , do you think that it is possible to replace some existing unfinished grammar in grammars-v4 with own implementation, I would like to share some grammars, system verilog and vhdl, however some non complete version of grammar is already present in grammars-v4.

KvanTTT commented 4 years ago

If your grammars cover more syntax than I think yes, it makes sense to replace existing unfinished grammars.

zhuzhzh commented 4 years ago

@Nic30 , do you know what's the best sv2017 grammer for ANTLR? just for syntax, it seems sv-parser(https://github.com/dalance/sv-parser) written in RUST is more complete.

Nic30 commented 4 years ago

@zhuzhzh As far as I know only sv-parser and hdlConvertor have complete sv grammar https://symbiflow.github.io/sv-tests/

zhuzhzh commented 4 years ago

@Nic30 in the compariosn result, I think sv-parser, veriable and surelog have the best coverage. From the public doc, they just generate CST instead of AST. I want to confirm hdlConvertor can generate CST.

Besides, I just have a glance at the grammer file in hdlConvertor. The line number of sv2017Parser.g4 is 2282. The line number of 658. The line number of verilogPreprocLexer.g4 is 338. The line number of verilogPreprocParser.g4 is 190. The sum is 3468. It's great that It's less than I thought.

Nic30 commented 4 years ago

@zhuzhzh This is a tricky question because lower layer antrl4 parser produces a CST, however the convertor layer which translates this low level CST to hdlAst generates AST. The AST representation is there because it is more easy to work with.

So the response is probably hdlConvertor can generate both.

zhuzhzh commented 4 years ago

Thanks for your patient answer, @Nic30