Segmentation in programming language grammars .. why?

rockbrentwood@gmail.com
Sun, 29 Dec 2019 21:05:27 -0800 (PST)

          From comp.compilers

Related articles
Segmentation in programming language grammars .. why? rockbrentwood@gmail.com (2019-12-29)
Re: Segmentation in programming language grammars .. why? 493-878-3164@kylheku.com (Kaz Kylheku) (2019-12-30)
| List of all articles for this month |

From: rockbrentwood@gmail.com
Newsgroups: comp.compilers
Date: Sun, 29 Dec 2019 21:05:27 -0800 (PST)
Organization: Compilers Central
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="8146"; mail-complaints-to="abuse@iecc.com"
Keywords: parse, design
Posted-Date: 30 Dec 2019 11:30:31 EST

I see this with many specifications -- redundant non-terminals and rules. I'll
illustrate it with a part of the grammar spec for C17.


Abbreviate opt, statement, labeled-statement, compound-statement,
expression-statement, selection-statement, iteration-statement,
jump-statement, block-item, block-item-list, declaration, expression,
constant-expression, identifier respectively as ?, S, Sx, Sc, Se, Sb, Sl, Sj,
SD, SDs, D, E, Ec, X for


The statement grammar is
(6.8) S -> Sx | Sc | Se | Sb | Sl | Sj
(6.8.1) Sx -> X ':' S
                Sx -> 'case' Ec ':' S
                Sx -> 'default' ':' S
(6.8.2) Sc -> '{' SDs? '}'
                SDs -> SD | SDs SD
                SD -> D | S
(6.8.3) Se -> E? ';'
(6.8.4) Sb -> 'if' '(' E ')' S
                Sb -> 'if' '(' E ')' S 'else' S
                Sb -> 'switch' '(' E ')' S
(6.8.5) Sl -> 'while' '(' E ')' S
                Sl -> 'do' S 'while' '(' E ')' ';'
                Sl -> 'for' '(' E? ';' E? ';' E? ')' S
                Sl -> 'for' '(' D E? ';' E? ')' S
(6.8.6) Sj -> 'goto' X ';'
                Sj -> 'continue' ';'
                Sj -> 'break' ';'
                Sj -> 'return' E? ';'


and is segmented into subgroups Sx, Sc, Se, Sb, Sl, Sj of S. Why? Why not just
write it as one segment like this? It's not creating new conflicts in so
doing.


(6.8)
(6.8.1) S -> X ':' S
                S -> 'case' Ec ':' S
                S -> 'default' ':' S
(6.8.2) S -> '{' SDs? '}'
(6.8.3) S -> E? ';'
                SDs -> SD | SDs SD
                SD -> D | S
(6.8.4) S -> 'if' '(' E ')' S
                S -> 'if' '(' E ')' S 'else' S
                S -> 'switch' '(' E ')' S
(6.8.5) S -> 'while' '(' E ')' S
                S -> 'do' S 'while' '(' E ')' ';'
                S -> 'for' '(' E? ';' E? ';' E? ')' S
                S -> 'for' '(' D E? ';' E? ')' S
(6.8.6) S -> 'goto' X ';'
                S -> 'continue' ';'
                S -> 'break' ';'
                S -> 'return' E? ';'


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.