Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.47 KB | None | 0 0
  1. grammar algol_starter;
  2.  
  3. options{backtrack=true;}
  4.  
  5. program: block | compound_statement;
  6.  
  7. empty: ;
  8.  
  9. basic_symbol: LETTER
  10. | DIGIT
  11. | logical_value
  12. | delimiter;
  13.  
  14.  
  15. LETTER: 'a' .. 'z'
  16. | 'A' .. 'Z';
  17.  
  18.  
  19. DIGIT: '0'..'9';
  20.  
  21. // logical_values
  22.  
  23. logical_value: 'true' | 'false';
  24.  
  25. // Delimiters
  26. delimiter: operator
  27. | separator
  28. | bracket
  29. | declarator
  30. | specificator;
  31.  
  32. operator:
  33. arithmetic_operator
  34. | relational_operator
  35. | logical_operator
  36. | sequential_operator;
  37.  
  38. arithmetic_operator: '+'
  39. | '−'
  40. | '×'
  41. | '/'
  42. | '÷'
  43. | '↑';
  44.  
  45. logical_operator: '≡'
  46. | '⊃'
  47. | '∨'
  48. | '∧'
  49. | '¬';
  50.  
  51. sequential_operator: 'go to'
  52. | 'if'
  53. | 'then'
  54. | 'else'
  55. | 'for'
  56. | 'do';
  57.  
  58. separator: ','
  59. | '.'
  60. | '⏨'
  61. | ':'
  62. | ';'
  63. | ':='
  64. | '␣'
  65. | 'step'
  66. | 'until'
  67. | 'while'
  68. | 'comment';
  69.  
  70. bracket: '('
  71. | ')'
  72. | '['
  73. | ']'
  74. | '`'
  75. | '\''
  76. | 'begin'
  77. | 'end';
  78.  
  79. declarator: 'own'
  80. | 'Boolean'
  81. | 'integer'
  82. | 'real'
  83. | 'array'
  84. | 'switch'
  85. | 'procedure';
  86.  
  87. specificator: 'string'
  88. | 'label'
  89. | 'value';
  90.  
  91. /*
  92.  
  93. ; comment <any sequence of zero or more characters not
  94. containing ;> ;
  95. equivalent to:
  96. ;
  97.  
  98. begin comment <any sequence of zero or more characters
  99. not containing ;> ;
  100. equivalent to:
  101. begin
  102. */
  103.  
  104. //Identifiers
  105. identifier: (LETTER) (LETTER | DIGIT)*;
  106.  
  107. // Numbers
  108. unsigned_integer: DIGIT+;
  109.  
  110. integer: unsigned_integer
  111. | '+' unsigned_integer
  112. | '−' unsigned_integer;
  113.  
  114. decimal_fraction: '.' unsigned_integer;
  115.  
  116. exponential_part: '⏨' integer;
  117.  
  118. decimal_number: unsigned_integer
  119. | decimal_fraction
  120. | unsigned_integer decimal_fraction;
  121.  
  122.  
  123. unsigned_number: decimal_number
  124. | decimal_fraction
  125. | decimal_number exponential_part;
  126.  
  127. number: unsigned_number
  128. | '+' unsigned_number
  129. | '−' unsigned_number;
  130.  
  131. //WORD : ~('\r' | '\n' | WS | '`' | '\'')+;
  132.  
  133. proper_string: LETTER*; //todo: put word*
  134. //<any sequence of characters not containing ` or '>
  135.  
  136. open_string: proper_string
  137. | proper_string closed_string open_string;
  138.  
  139. closed_string:
  140. '`' open_string '\'';
  141.  
  142. string: closed_string
  143. | closed_string string;
  144.  
  145. //Expressions
  146. expression: arithmetic_expression
  147. | boolean_expression
  148. | designational_expression;
  149.  
  150. // Variables
  151. variable_identifier: identifier;
  152.  
  153. simple_variable: variable_identifier;
  154.  
  155. subscript_expression: arithmetic_expression;
  156.  
  157. /*
  158. subscript_list: subscript_expression
  159. | subscript_list ',' subscript_expression;
  160. */
  161.  
  162. subscript_list: subscript_expression (',' subscript_expression)*;
  163.  
  164. array_identifier: identifier;
  165.  
  166. subscripted_variable: array_identifier '[' subscript_list ']';
  167.  
  168. variable: simple_variable
  169. | subscripted_variable;
  170.  
  171. //Function designators
  172. procedure_identifier: identifier;
  173.  
  174. actual_parameter: string
  175. | expression
  176. | array_identifier
  177. | switch_identifier
  178. | procedure_identifier;
  179.  
  180. letter_string: LETTER+;
  181.  
  182. parameter_delimiter: ','
  183. | ')' letter_string ':' '(';
  184.  
  185. /*
  186. actual_parameter_list: actual_parameter
  187. | actual_parameter_list parameter_delimiter actual_parameter;
  188. */
  189.  
  190. actual_parameter_list: actual_parameter (parameter_delimiter actual_parameter)*;
  191.  
  192. actual_parameter_part: empty
  193. | '(' actual_parameter_list ')';
  194.  
  195. function_designator: procedure_identifier actual_parameter_part;
  196.  
  197. //Arithmetic expressions
  198. adding_operator: '+' | '−';
  199.  
  200. multiplying_operator: '×'
  201. | '/'
  202. | '÷';
  203.  
  204. primary: unsigned_number
  205. | variable
  206. | function_designator
  207. | '(' arithmetic_expression ')';
  208.  
  209. /*
  210. factor: primary
  211. | factor
  212. | factor '↑' primary;
  213. */
  214.  
  215. factor: primary ('↑' primary)*;
  216.  
  217. /*
  218. term: factor
  219. | term multiplying_operator factor;
  220. */
  221.  
  222. term: factor (multiplying_operator factor)*;
  223.  
  224. /*
  225. simple_arithmetic_expression: term
  226. | adding_operator term
  227. | simple_arithmetic_expression adding_operator term;
  228. */
  229.  
  230. simple_arithmetic_expression: adding_operator? term (adding_operator term)*;
  231.  
  232.  
  233. arithmetic_expression: simple_arithmetic_expression
  234. | if_clause simple_arithmetic_expression 'else' arithmetic_expression;
  235.  
  236. //Boolean expressions
  237. relational_operator: '<'
  238. | '≤'
  239. | '='
  240. | '≥'
  241. | '>'
  242. | '≠';
  243.  
  244. relation: simple_arithmetic_expression relational_operator simple_arithmetic_expression;
  245.  
  246. boolean_primary: logical_value
  247. | variable
  248. | function_designator
  249. | relation
  250. | '(' boolean_expression ')';
  251.  
  252. boolean_secondary: boolean_primary
  253. | '¬' boolean_primary;
  254.  
  255. /*
  256. boolean_factor: boolean_secondary
  257. | boolean_factor '∧' boolean_secondary;
  258. */
  259.  
  260. boolean_factor: boolean_secondary ('∧' boolean_secondary)*;
  261.  
  262. /*
  263. boolean_term: boolean_factor
  264. | boolean_term '∨' boolean_factor;
  265. */
  266.  
  267.  
  268. boolean_term: boolean_factor ('∨' boolean_factor)*;
  269.  
  270. implication: boolean_term ('⊃' boolean_term)*;
  271.  
  272. /*
  273. simple_boolean: implication
  274. | simple_boolean '≡' implication;
  275. */
  276.  
  277. simple_boolean: implication ('≡' implication)*;
  278.  
  279. boolean_expression: simple_boolean
  280. | if_clause simple_boolean 'else' boolean_expression;
  281.  
  282. //Designational expressions
  283. label: identifier
  284. | unsigned_integer;
  285.  
  286. switch_identifier: identifier;
  287.  
  288. switch_designator: switch_identifier '[' subscript_expression ']';
  289.  
  290. simple_designational_expression: label
  291. | switch_designator
  292. | '(' designational_expression ')';
  293.  
  294. designational_expression: simple_designational_expression
  295. | if_clause simple_designational_expression 'else' designational_expression;
  296.  
  297. // Compound statements and blocks
  298. unlabelled_basic_statement: assignment_statement
  299. | go_to_statement
  300. | dummy_statement
  301. | procedure_statement;
  302.  
  303. basic_statement:
  304. unlabelled_basic_statement
  305. | label ':' basic_statement;
  306.  
  307. statement: unconditional_statement
  308. | conditional_statement
  309. | for_statement;
  310.  
  311. compound_tail: statement 'end'
  312. | statement ';' compound_tail;
  313.  
  314. /*
  315. block_head: 'begin' declaration
  316. | block_head ';' declaration;
  317. */
  318.  
  319. block_head: 'begin' declaration (';' declaration)*;
  320.  
  321. unlabelled_block: block_head ';' compound_tail;
  322.  
  323. unlabelled_compound: 'begin' compound_tail;
  324.  
  325. compound_statement: unlabelled_compound
  326. | label ':' compound_statement;
  327.  
  328. block: unlabelled_block
  329. | label ':' block;
  330.  
  331.  
  332.  
  333. //Assignment statements
  334. destination: variable
  335. | procedure_identifier;
  336.  
  337. left_part: destination ':=';
  338.  
  339. /*
  340. left_part_list: left_part
  341. | left_part_list left_part;
  342. */
  343.  
  344. left_part_list: left_part+;
  345.  
  346. assignment_statement: left_part_list arithmetic_expression
  347. | left_part_list boolean_expression;
  348.  
  349. // Go to statements
  350. go_to_statement: 'go' 'to' designational_expression;
  351.  
  352. // Dummy statements
  353. dummy_statement: empty;
  354.  
  355. // Conditional statements
  356. if_clause: 'if' boolean_expression 'then';
  357.  
  358. unconditional_statement: basic_statement
  359. | compound_statement
  360. | block;
  361.  
  362. if_statement: if_clause unconditional_statement;
  363.  
  364. conditional_statement: if_statement
  365. | if_statement 'else' statement
  366. | if_clause for_statement
  367. | block ':' conditional_statement;
  368. //<label> : <conditional_statement> dans BNF
  369.  
  370. // For statements
  371. for_list_element: arithmetic_expression
  372. | arithmetic_expression 'step' arithmetic_expression 'until' arithmetic_expression
  373. | arithmetic_expression 'while' boolean_expression;
  374.  
  375. /*
  376. for_list: for_list_element
  377. | for_list ',' for_list_element;
  378. */
  379.  
  380. for_list: for_list_element (',' for_list_element)*;
  381.  
  382. for_clause:
  383. 'for' variable ':=' for_list 'do';
  384.  
  385. for_statement: for_clause statement
  386. | label ':' for_statement;
  387.  
  388. // Procedure statements
  389.  
  390. procedure_statement: procedure_identifier actual_parameter_part;
  391.  
  392. // Declarations
  393. declaration: type_declaration
  394. | array_declaration
  395. | switch_declaration
  396. | procedure_declaration;
  397.  
  398. // Type declarations
  399. type_list: simple_variable
  400. | simple_variable ',' type_list;
  401.  
  402. type: 'real'
  403. | 'integer'
  404. | 'Boolean';
  405.  
  406. local_or_own: empty
  407. | 'own';
  408.  
  409. type_declaration: local_or_own type type_list;
  410.  
  411. // Array declarations
  412. lower_bound: arithmetic_expression;
  413.  
  414. upper_bound: arithmetic_expression;
  415.  
  416. bound_pair: lower_bound ':' upper_bound;
  417.  
  418. /*
  419. bound_pair_list: bound_pair
  420. | bound_pair_list ',' bound_pair;
  421. */
  422.  
  423. bound_pair_list: bound_pair (',' bound_pair)*;
  424.  
  425. array_segment: array_identifier '[' bound_pair_list ']'
  426. | array_identifier ',' array_segment;
  427.  
  428. /*
  429. array_list: array_segment
  430. | array_list ',' array_segment;
  431. */
  432.  
  433. array_list: array_segment (',' array_segment)*;
  434.  
  435. array_declarer:type 'array'
  436. | 'array';
  437.  
  438. array_declaration: local_or_own array_declarer array_list;
  439.  
  440. // Switch declarations
  441. /*
  442. switch_list: designational_expression
  443. | switch_list ',' designational_expression;
  444. */
  445.  
  446. switch_list: designational_expression (',' designational_expression)*;
  447.  
  448. switch_declaration: 'switch' switch_identifier ':=' switch_list;
  449.  
  450. // Procedure declarations
  451.  
  452. formal_parameter: identifier;
  453.  
  454. /*
  455. formal_parameter_list: formal_parameter
  456. | formal_parameter_list parameter_delimiter formal_parameter;
  457. */
  458.  
  459. formal_parameter_list: formal_parameter (parameter_delimiter formal_parameter)*;
  460.  
  461. formal_parameter_part: empty
  462. | '(' formal_parameter_list ')';
  463.  
  464. /*
  465. identifier_list: identifier
  466. | identifier_list ',' identifier;
  467. */
  468.  
  469. identifier_list: identifier (',' identifier)*;
  470.  
  471. value_part: 'value' identifier_list ';'
  472. | empty;
  473.  
  474. specifier: 'string'
  475. | type
  476. | array_declarer
  477. | 'label'
  478. | 'switch'
  479. | 'procedure'
  480. | type 'procedure';
  481.  
  482. /*
  483. specification_part: empty
  484. | specifier identifier_list ';'
  485. | specification_part specifier identifier_list;
  486. */
  487.  
  488. specification_part: (specifier identifier_list ';')? (specifier identifier_list)*;
  489.  
  490. procedure_heading: procedure_identifier formal_parameter_part ';' value_part specification_part;
  491.  
  492. procedure_body: statement;
  493.  
  494. procedure_declaration: 'procedure' procedure_heading procedure_body
  495. | type 'procedure' procedure_heading procedure_body;
  496.  
  497.  
  498. WS : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ {$channel=HIDDEN;} ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement