Skip to content

Commit 860e9d9

Browse files
authored
import section grammar and parser
1 parent 887efaa commit 860e9d9

File tree

2 files changed

+355
-0
lines changed

2 files changed

+355
-0
lines changed

grammar/imp-grammar.gll

+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2+
* M2C Modula-2 Compiler & Translator *
3+
* *
4+
* Copyright (c) 2015-2023 Benjamin Kowarsch *
5+
* *
6+
* @synopsis *
7+
* *
8+
* M2C is a portable Modula-2 to C translator and via-C compiler for the *
9+
* bootstrap subset of the revised Modula-2 language described in *
10+
* *
11+
* https://github.com/m2sf/m2bsk/wiki/Language-Specification *
12+
* *
13+
* In translator mode, M2C translates Modula-2 source files to semantically *
14+
* equivalent C source files. In compiler mode, it translates the Modula-2 *
15+
* source files to C, then compiles the resulting C sources to object and *
16+
* executable files using the host system's resident C compiler and linker. *
17+
* *
18+
* Further information at https://github.com/m2sf/m2c/wiki *
19+
* *
20+
* @file *
21+
* *
22+
* imp-grammar.gll *
23+
* *
24+
* EBNF grammar of Modula-2 module header and import section. *
25+
* *
26+
* @license *
27+
* *
28+
* M2C is free software: You can redistribute and modify it under the terms *
29+
* of the GNU Lesser General Public License (LGPL) either version 2.1 or at *
30+
* your choice version 3, both published by the Free Software Foundation. *
31+
* *
32+
* M2C is distributed in the hope it may be useful, but strictly WITHOUT ANY *
33+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
34+
* FOR ANY PARTICULAR PURPOSE. Read the license for more details. *
35+
* *
36+
* You should have received a copy of the GNU Lesser General Public License *
37+
* along with M2C. If not, see <https://www.gnu.org/copyleft/lesser.html>. *
38+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
39+
40+
grammar m2import;
41+
42+
/* * * R e s e r v e d W o r d s * * */
43+
44+
reserved
45+
BEGIN, CONST, DEFINITION, END, IMPLEMENTATION, IMPORT, MODULE, PROCEDURE,
46+
TO, TYPE, VAR;
47+
48+
49+
/* * * N o n - T e r m i n a l S y m b o l s * * */
50+
51+
/* Module Header and Import */
52+
53+
moduleHeaderAndImport :=
54+
defModHdrAndImport | impOrPgmModHdrAndImport
55+
;
56+
57+
/* Definition Module Header and Import */
58+
59+
defModHdrAndImport :=
60+
DEFINITION MODULE moduleIdent ';'
61+
import* endOfDefModImport;
62+
;
63+
64+
alias moduleIdent = Ident ;
65+
66+
67+
/* Import */
68+
69+
import :=
70+
IMPORT libIdent reExport? ( ',' libIdent reExport? )* ';'
71+
;
72+
73+
alias libIdent = Ident ;
74+
75+
alias reExport = '+' ;
76+
77+
78+
/* Implementation or Program Module Header and Import */
79+
80+
impOrPgmModHdrAndImport :=
81+
IMPLEMENTATION? MODULE moduleIdent ';'
82+
privateImport* endOfImpAndPgmModImport;
83+
;
84+
85+
86+
/* Private Import */
87+
88+
privateImport :=
89+
IMPORT moduleList
90+
;
91+
92+
alias moduleList = identList;
93+
94+
95+
/* Identifier List */
96+
97+
identList :=
98+
Ident ( ',' Ident )*
99+
;
100+
101+
102+
/* End of Definition Module Import */
103+
104+
endOfDefModImport :=
105+
CONST | TYPE | VAR | PROCEDURE | TO | EndOfFile
106+
;
107+
108+
109+
/* End of Implementation and Program Module Import */
110+
111+
endOfImpAndPgmModImport :=
112+
BEGIN | endOfDefModImport
113+
;
114+
115+
116+
/* * * T e r m i n a l S y m b o l s * * */
117+
118+
/* Identifier */
119+
120+
Ident :=
121+
Letter ( Letter | Digit )*
122+
;
123+
124+
125+
/* * * I g n o r e S y m b o l s * * */
126+
127+
/* Whitespace */
128+
129+
Whitespace :=
130+
Space | Tabulator
131+
;
132+
133+
.Tabulator := 0u9 ;
134+
135+
136+
/* Line Comment */
137+
138+
LineComment :=
139+
'!' AnyPrintable* EndOfLine
140+
;
141+
142+
.AnyPrintable := 0u20 .. 0u7E ; /* greedy */
143+
144+
145+
/* Block Comment */
146+
147+
BlockComment :=
148+
'(*' ( AnyPrintable | BlockComment | EndOfLine )* '*)'
149+
;
150+
151+
152+
/* Pragma */
153+
154+
Pragma :=
155+
'<*' ( AnyPrintable | EndOfLine )* '*>'
156+
;
157+
158+
159+
/* Disabled Code Section */
160+
161+
DisabledCodeSection :=
162+
'?<' /* strictly in first column of a line */
163+
( AnyPrintable | BlockComment | EndOfLine )*
164+
'>?' /* strictly in first column of a line */
165+
;
166+
167+
168+
/* End of Line Marker */
169+
170+
EndOfLine :=
171+
LF | CR ( LF )?
172+
;
173+
174+
.LF := 0uA ;
175+
176+
.CR := 0uD ;
177+
178+
endg m2import.
179+
180+
/* END OF FILE */

src/imp/m2c-import-parser.c

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2+
* M2C Modula-2 Compiler & Translator *
3+
* *
4+
* Copyright (c) 2015-2023 Benjamin Kowarsch *
5+
* *
6+
* @synopsis *
7+
* *
8+
* M2C is a portable Modula-2 to C translator and via-C compiler for the *
9+
* bootstrap subset of the revised Modula-2 language described in *
10+
* *
11+
* https://github.com/m2sf/m2bsk/wiki/Language-Specification *
12+
* *
13+
* In translator mode, M2C translates Modula-2 source files to semantically *
14+
* equivalent C source files. In compiler mode, it translates the Modula-2 *
15+
* source files to C, then compiles the resulting C sources to object and *
16+
* executable files using the host system's resident C compiler and linker. *
17+
* *
18+
* Further information at https://github.com/m2sf/m2c/wiki *
19+
* *
20+
* @file *
21+
* *
22+
* m2c-import-parser.c *
23+
* *
24+
* Implementation of identifier to snake-case translation dictionary. *
25+
* *
26+
* @license *
27+
* *
28+
* M2C is free software: You can redistribute and modify it under the terms *
29+
* of the GNU Lesser General Public License (LGPL) either version 2.1 or at *
30+
* your choice version 3, both published by the Free Software Foundation. *
31+
* *
32+
* M2C is distributed in the hope it may be useful, but strictly WITHOUT ANY *
33+
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
34+
* FOR ANY PARTICULAR PURPOSE. Read the license for more details. *
35+
* *
36+
* You should have received a copy of the GNU Lesser General Public License *
37+
* along with M2C. If not, see <https://www.gnu.org/copyleft/lesser.html>. *
38+
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
39+
40+
static void m2c_parse_file ();
41+
42+
static void parse_start_symbol ();
43+
44+
45+
/* --------------------------------------------------------------------------
46+
* private function module_header_and_import()
47+
* --------------------------------------------------------------------------
48+
* moduleHeaderAndImport :=
49+
* defModHdrAndImport | impOrPgmModHdrAndImport
50+
* ;
51+
* ----------------------------------------------------------------------- */
52+
53+
static m2c_token_t module_header_and_import () {
54+
m2c_token_t lookahead;
55+
56+
lookahead = m2c_next_sym();
57+
58+
switch (lookahead) {
59+
case TOKEN_DEFINITION :
60+
lookahead = definition_module(p);
61+
break;
62+
63+
case TOKEN_IMPLEMENTATION :
64+
lookahead = implementation_module(p);
65+
break;
66+
67+
case TOKEN_MODULE:
68+
lookahead = program_module(p);
69+
break;
70+
} /* end switch */
71+
72+
return lookahead;
73+
} /* end module_header_and_import */
74+
75+
76+
/* --------------------------------------------------------------------------
77+
* private function def_mod_hdr_and_import()
78+
* --------------------------------------------------------------------------
79+
* defModHdrAndImport :=
80+
* DEFINITION MODULE moduleIdent ';'
81+
* import* endOfDefModImport;
82+
* ;
83+
*
84+
* alias moduleIdent = Ident ;
85+
*
86+
* endOfDefModImport := CONST | TYPE | VAR | PROCEDURE | TO | EndOfFile ;
87+
* ----------------------------------------------------------------------- */
88+
89+
static m2c_token_t def_mod_hdr_and_import () {
90+
m2c_token_t lookahead;
91+
92+
93+
94+
return lookahead;
95+
} /* end def_mod_hdr_and_import */
96+
97+
98+
/* --------------------------------------------------------------------------
99+
* private function import()
100+
* --------------------------------------------------------------------------
101+
* import :=
102+
* IMPORT libIdent reExport? ( ',' libIdent reExport? )* ';'
103+
* ;
104+
*
105+
* alias libIdent = Ident ;
106+
*
107+
* alias reExport = '+' ;
108+
* ----------------------------------------------------------------------- */
109+
110+
static m2c_token_t import () {
111+
m2c_token_t lookahead;
112+
113+
114+
115+
return lookahead;
116+
} /* end import */
117+
118+
119+
/* --------------------------------------------------------------------------
120+
* private function imp_or_pgm_mod_hdr_and_import()
121+
* --------------------------------------------------------------------------
122+
* impOrPgmModHdrAndImport :=
123+
* IMPLEMENTATION? MODULE moduleIdent ';'
124+
* privateImport* endOfImpAndPgmModImport;
125+
* ;
126+
*
127+
* endOfImpAndPgmModImport := BEGIN | endOfDefModImport ;
128+
* ----------------------------------------------------------------------- */
129+
130+
static m2c_token_t imp_or_pgm_mod_hdr_and_import () {
131+
m2c_token_t lookahead;
132+
133+
134+
135+
return lookahead;
136+
} /* end imp_or_pgm_mod_hdr_and_import */
137+
138+
139+
/* --------------------------------------------------------------------------
140+
* private function private_import()
141+
* --------------------------------------------------------------------------
142+
* privateImport :=
143+
* IMPORT moduleList
144+
* ;
145+
*
146+
* alias moduleList = identList;
147+
* ----------------------------------------------------------------------- */
148+
149+
static m2c_token_t private_import () {
150+
m2c_token_t lookahead;
151+
152+
153+
154+
return lookahead;
155+
} /* end private_import */
156+
157+
158+
/* --------------------------------------------------------------------------
159+
* private function ident_list()
160+
* --------------------------------------------------------------------------
161+
* identList :=
162+
* Ident ( ',' Ident )*
163+
* ;
164+
* ----------------------------------------------------------------------- */
165+
166+
static m2c_token_t ident_list () {
167+
m2c_token_t lookahead;
168+
169+
170+
171+
return lookahead;
172+
} /* end ident_list */
173+
174+
175+
/* END OF FILE */

0 commit comments

Comments
 (0)