Advertisement
KDOXG

Lexicon + Syntax Parser

Sep 19th, 2022 (edited)
1,117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         // javacc -NOSTATIC Karloff.jj
  9.         // pasta dos trabalhos: https://drive.google.com/drive/folders/1WRvFKckGv7ciZ9fAlITJIOxZzMp0Jcln?usp=sharing
  10.         Karloff parser = new Karloff(new FileReader(args[0]));
  11.         parser.KarloffLexicon();
  12.         parser = new Karloff(new FileReader(args[0]));
  13.         parser.Karloff();
  14.     }
  15.  
  16. }
  17.  
  18. PARSER_END(Karloff)
  19.  
  20. SKIP:{
  21.   " "
  22. | "\t"
  23. | "\n"
  24. | "\r"}
  25.  
  26. TOKEN:{
  27.   <MAIN:"main">
  28. | <ACHAVES:"{">
  29. | <FCHAVES:"}">
  30. | <APAREN:"(">
  31. | <FPAREN:")">
  32. | <SEMIC:";">
  33. | <COMMA:",">
  34. | <FUNC:"func">
  35. | <TVOID:"void">
  36. | <NEWVAR:"newVar">
  37. | <TINT:"int">
  38. | <TBOOL:"bool">
  39. | <IF:"if">
  40. | <THEN:"then">
  41. | <WHILE:"while">
  42. | <REPEAT:"repeat">
  43. | <UNTIL:"until">
  44. | <RETURN:"return">
  45. | <TRUE:"true">
  46. | <FALSE:"false">
  47. | <OUTPUT:"System.output">
  48. | <INPUT:"System.input">
  49. | <PLUS:"+">
  50. | <MINUS:"-">
  51. | <ASTERISK:"*">
  52. | <SLASH:"/">
  53. | <AMP:"&">
  54. | <VERBAR:"|">
  55. | <LESS:"<">
  56. | <GREATER:">">
  57. | <ASSIGN:"=">
  58. | <EQUAL:"==">
  59.  
  60. }
  61.  
  62. TOKEN:{
  63.   <NUM: (["0"-"9"] | ["1"-"9"] (["0"-"9"])*) ("." (["0"-"9"])* ["1"-"9"])? ("E" (["+","-"])? ["1"-"9"] (["0"-"9"])* )?>
  64. | <ID: ["a"-"z","A"-"Z"] (("_")?["a"-"z","A"-"Z","0"-"9"])* >
  65.  
  66. }
  67.  
  68. void Karloff () :
  69. {Token t;}
  70. {
  71.     Main() Func() <EOF> {System.out.println("Succesful!");}
  72. }
  73.  
  74. void Main () :
  75. {}
  76. {
  77.     <TVOID> <MAIN> <ACHAVES> Vardecl() Seqcomandos() <FCHAVES>
  78. }
  79.  
  80. void Vardecl () :
  81. {}
  82. {
  83.     (<NEWVAR> Tipo() <ID> <SEMIC>)*
  84. }
  85.  
  86. void Tipo () :
  87. {}
  88. {
  89.     <TBOOL> | <TINT>
  90. }
  91.  
  92. void Seqcomandos () :
  93. {}
  94. {
  95.     (Comando() <SEMIC>)*
  96. }
  97.  
  98. void Comando () :
  99. {}
  100. {
  101.   <ID> (<ASSIGN> Exp() | <APAREN> ( Listaexp() )? <FPAREN>)
  102. | <IF> <APAREN> Exp() <FPAREN> <THEN> <ACHAVES> Seqcomandos() <FCHAVES>
  103. | <WHILE> <APAREN> Exp() <FPAREN> <ACHAVES> Seqcomandos() <FCHAVES>
  104. | <REPEAT> <ACHAVES> Seqcomandos() <FCHAVES> <UNTIL> <APAREN> Exp() <FPAREN>
  105. | <RETURN> Exp()
  106. | <OUTPUT> <APAREN> Exp() <FPAREN>
  107. | <INPUT> <APAREN> Exp() <FPAREN>
  108. }
  109.  
  110. void Exp () :
  111. {}
  112. {
  113.     <APAREN> Exp() Op() Exp() <FPAREN>
  114. |   Fator()
  115. }
  116.  
  117. void Fator () :
  118. {}
  119. {
  120.     <ID> ( <APAREN> ( Listaexp() )? <FPAREN> )?
  121. |   <NUM>
  122. |   <TRUE>
  123. |   <FALSE>
  124. }
  125.  
  126. void Op () :
  127. {}
  128. {
  129.     <PLUS> | <MINUS> | <ASTERISK> | <SLASH>  | <AMP>
  130. | <VERBAR> | <LESS>  | <GREATER>  | <ASSIGN> | <EQUAL>
  131. }
  132.  
  133. void Listaexp () :
  134. {}
  135. {
  136.     Exp() ( <COMMA> Exp() )*
  137. }
  138.  
  139. void Func () :
  140. {}
  141. {
  142.     ( <FUNC> Tipo() <ID> <APAREN> (Listaarg())? <FPAREN> <ACHAVES> Vardecl() Seqcomandos() <FCHAVES> )*
  143. }
  144.  
  145. void Listaarg () :
  146. {}
  147. {
  148.     Tipo() <ID> ( <COMMA> Tipo() <ID> )*
  149. }
  150.  
  151. void KarloffLexicon () :
  152. {Token t;}
  153. {
  154.  
  155. (
  156.   <MAIN> { System.out.println("Palavra reservada: main");}
  157. | <ACHAVES> { System.out.println("Abre chaves: {");}
  158. | <FCHAVES> { System.out.println("Fecha chaves: }");}
  159. | <APAREN> { System.out.println("Abre parenteses: (");}
  160. | <FPAREN> { System.out.println("Fecha parenteses: )");}
  161. | <SEMIC> { System.out.println("Ponto e virgula: ;");}
  162. | <COMMA> { System.out.println("Virgula: ,");}
  163. | <FUNC> { System.out.println("Palavra reservada: func");}
  164. | <TVOID> { System.out.println("Palavra reservada: void");}
  165. | <NEWVAR> { System.out.println("Palavra reservada: newVar");}
  166. | <TINT> { System.out.println("Palavra reservada: int");}
  167. | <IF> { System.out.println("Palavra reservada: if");}
  168. | <THEN> { System.out.println("Palavra reservada: then");}
  169. | <WHILE> { System.out.println("Palavra reservada: while");}
  170. | <REPEAT> { System.out.println("Palavra reservada: repeat");}
  171. | <UNTIL> { System.out.println("Palavra reservada: until");}
  172. | <RETURN> { System.out.println("Palavra reservada: return");}
  173. | <TRUE> { System.out.println("Palavra reservada: true");}
  174. | <FALSE> { System.out.println("Palavra reservada: false");}
  175. | <OUTPUT> { System.out.println("Palavra reservada: System.output");}
  176. | <INPUT> { System.out.println("Palavra reservada: System.input");}
  177. | <PLUS> { System.out.println("Operador 'mais': +");}
  178. | <MINUS> { System.out.println("Operador 'menos': -");}
  179. | <ASTERISK> { System.out.println("Operador 'asterisco': *");}
  180. | <SLASH> { System.out.println("Operador 'barra': /");}
  181. | <AMP> { System.out.println("Operador 'e comercial': &");}
  182. | <VERBAR> { System.out.println("Operador 'pipe': |");}
  183. | <LESS> { System.out.println("Operador 'menor': <");}
  184. | <GREATER> { System.out.println("Operador 'maior': >");}
  185. | <ASSIGN> { System.out.println("Operador 'atribuicao': =");}
  186. | <EQUAL> { System.out.println("Operador 'igualdade': ==");}
  187. | t=<NUM> { System.out.println("NĂºmero: "+ t.image);}
  188. | t=<ID> { System.out.println("Identificador: "+ t.image);}
  189. )*
  190. <EOF>
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement