Advertisement
KDOXG

Syntax

Sep 2nd, 2022 (edited)
1,300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. PARSER_BEGIN(Karloff)
  2.  
  3. import java.io.*;
  4.  
  5. public class Karloff {
  6.  
  7.     public static void main(String args[]) throws ParseException, IOException {
  8.  
  9.         Karloff parser = new Karloff(new FileInputStream(args[0]));
  10.         parser.Karloff();
  11.     }
  12.  
  13. }
  14.  
  15. PARSER_END(Karloff)
  16.  
  17. SKIP:{
  18.   " "
  19. | "\t"
  20. | "\n"
  21. | "\r"}
  22.  
  23. TOKEN:{
  24.   <MAIN:"main">
  25. | <ACHAVES:"{">
  26. | <FCHAVES:"}">
  27. | <APAREN:"(">
  28. | <FPAREN:")">
  29. | <SEMIC:";">
  30. | <COMMA:",">
  31. | <FUNC:"func">
  32. | <TVOID:"void">
  33. | <NEWVAR:"newVar">
  34. | <TINT:"int">
  35. | <TBOOL:"bool">
  36. | <IF:"if">
  37. | <THEN:"then">
  38. | <WHILE:"while">
  39. | <REPEAT:"repeat">
  40. | <UNTIL:"until">
  41. | <RETURN:"return">
  42. | <TRUE:"true">
  43. | <FALSE:"false">
  44. | <OUTPUT:"System.output">
  45. | <INPUT:"System.input">
  46. | <PLUS:"+">
  47. | <MINUS:"-">
  48. | <ASTERISK:"*">
  49. | <SLASH:"/">
  50. | <AMP:"&">
  51. | <VERBAR:"|">
  52. | <LESS:"<">
  53. | <GREATER:">">
  54. | <ASSIGN:"=">
  55. | <EQUAL:"==">
  56.  
  57. }
  58.  
  59. TOKEN:{
  60.   <NUM: (["0"-"9"] | ["1"-"9"] (["0"-"9"])*) ("." (["0"-"9"])* ["1"-"9"])? ("E" (["+","-"])? ["1"-"9"] (["0"-"9"])* )?>
  61. | <ID: ["a"-"z","A"-"Z"] (("_")?["a"-"z","A"-"Z","0"-"9"])* >
  62.  
  63. }
  64.  
  65. void Karloff () :
  66. {Token t;}
  67. {
  68.     Main() Func() <EOF> {System.out.println("Succesful!");}
  69. }
  70.  
  71. void Main () :
  72. {}
  73. {
  74.     <TVOID> <MAIN> <ACHAVES> Vardecl() Seqcomandos() <FCHAVES>
  75. }
  76.  
  77. void Vardecl () :
  78. {}
  79. {
  80.     (<NEWVAR> Tipo() <ID> <SEMIC>)*
  81. }
  82.  
  83. void Tipo () :
  84. {}
  85. {
  86.     <TBOOL> | <TINT>
  87. }
  88.  
  89. void Seqcomandos () :
  90. {}
  91. {
  92.     (Comando() <SEMIC>)*
  93. }
  94.  
  95. void Comando () :
  96. {}
  97. {
  98.   <ID> (<ASSIGN> Exp() | <APAREN> ( Listaexp() )? <FPAREN>)
  99. | <IF> <APAREN> Exp() <FPAREN> <THEN> <ACHAVES> Seqcomandos() <FCHAVES>
  100. | <WHILE> <APAREN> Exp() <FPAREN> <ACHAVES> Seqcomandos() <FCHAVES>
  101. | <REPEAT> <ACHAVES> Seqcomandos() <FCHAVES> <UNTIL> <APAREN> Exp() <FPAREN>
  102. | <RETURN> Exp()
  103. | <OUTPUT> <APAREN> Exp() <FPAREN>
  104. | <INPUT> <APAREN> Exp() <FPAREN>
  105. }
  106.  
  107. void Exp () :
  108. {}
  109. {
  110.     <APAREN> Exp() Op() Exp() <FPAREN>
  111. |   Fator()
  112. }
  113.  
  114. void Fator () :
  115. {}
  116. {
  117.     <ID> ( <APAREN> ( Listaexp() )? <FPAREN> )?
  118. |   <NUM>
  119. |   <TRUE>
  120. |   <FALSE>
  121. }
  122.  
  123. void Op () :
  124. {}
  125. {
  126.     <PLUS> | <MINUS> | <ASTERISK> | <SLASH>  | <AMP>
  127. | <VERBAR> | <LESS>  | <GREATER>  | <ASSIGN> | <EQUAL>
  128. }
  129.  
  130. void Listaexp () :
  131. {}
  132. {
  133.     Exp() ( <COMMA> Exp() )*
  134. }
  135.  
  136. void Func () :
  137. {}
  138. {
  139.     ( <FUNC> Tipo() <ID> <APAREN> (Listaarg())? <FPAREN> <ACHAVES> Vardecl() Seqcomandos() <FCHAVES> )*
  140. }
  141.  
  142. void Listaarg () :
  143. {}
  144. {
  145.     Tipo() <ID> ( <COMMA> Tipo() <ID> )*
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement