简体   繁体   中英

Intellij idea BNF end of line

I use to describe your parameter file BNF.

root ::= commands *
private commands ::= !<<eof>> (f_command | comments) {string_variable}*
comments ::= LINE_COMMENT
f_command ::= F

How can I get to realize BNF end of the line? Because the BNF can not understand where the end of the parameter F and where the end of the parameter Comments (options parameter may be many). By analogy with the << eof >>.

Example parameters file:

F option1 option2 optionN
C option1 option2 optionN

I'm not exactly sure what the problem your specifying is, but I have a few questions about some other things that look wrong to me- maybe one of those is it!

  1. Why do you have !<<eof>> in the production for 'private commands' ? does this mean "eof cannot be the first symbol"? if so, that seems weird, and if you needed that for <<eof>> then wouldn't you also need it for every other invalid starting character, and wouldn't it suggest that it IS valid for any of the other productions to start with <<eof>> ? As I can see, the only place that <<eof>> makes sense is in the very first production, maybe like this:

    root ::= commands<<eof>>

  2. it also seems strange to me that you are including comments in your grammar. If they are comments then your parser doesn't need them, right? filter them out at the lexical analysis stage, then you can ensure that only valid tokens get through to the parser. Unless, of course, you are creating some system I can't conceive in which comments ARE valid tokens, and are needed by the parser :).

Also don't use '*' for repetition (if that is what you're doing)- you have to factor it into the grammar. I think what you want is for the grammar to say "one or more consecutive 'commands' productions is allowed" if so, then one way to achieve this could be as follows:

root ::= commands<<eof>>
commands ::= commandsX
X ::= private_command | epsilon
private_command ::= <whatever you want this to be>

I think that is closer to what you want. Keep in mind that commands ::= commandsX is left recursive, so you'll have to fix that if you need an LL grammar. If you want private_command to accept any variable number of options, just use a similar approach when defining the productions for private_command .

I am by no means an expert at this stuff, just something I'm interested in, so I could be wrong about some of that. corrections welcome!

Some good BNF references:

http://marvin.cs.uidaho.edu/Teaching/CS445/grammar.html

https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form

NOt sure what you application for this is, but if the intention is just to get a working parser you could look into bison / yacc (two very similar tools with diff. names, for parsing) and lex / flex (again, two similar programs with diff. names, for lexing). They will write the parser and lexer code for you, but you need a sane grammar to give them.

https://github.com/westes/flex

https://www.gnu.org/software/bison/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM