SHARE
TWEET

Paiva

a guest Mar 24th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. %{
  2. //---------------------------------------------
  3. //     João Barros 2016241579
  4. //     Ricardo Paiva 2016253100
  5. //---------------------------------------------
  6. #include<stdio.h>
  7. int nlines=1;
  8. int linha_aux=0, coluna_aux=0;
  9. int ncolunas=1;
  10. int semicolon=0;
  11. char* string;
  12. int ver_erro = 0;
  13. %}
  14.  
  15.  
  16. %x COMENTARIO ASPAS COMENTARIO_LINHA
  17.  
  18. reserved    "default"|"++"|"--"|"select"|"go"|"struct"|"chan"|"goto"|"break"|"interface"|"case"|"defer"|"map"|"switch"|"const"|"fallthrough"|"range"|"type"|"continue"|"import"
  19. numero  [0-9]+
  20. exp "e"|"E"
  21. sinal   "-"|"+"
  22. letra   [a-zA-Z_][0-9_a-zA-Z]*
  23. reallit ([0-9]+"."([0-9]+)?(("e"|"E")("-"|"+")?[0-9]+)?)|("."[0-9]+(("e"|"E")("-"|"+")?[0-9]+)?)|([0-9]+("e"|"E")("-"|"+")?[0-9]+)
  24. intlit  [0-9]+|[0-9]*(0x|0X)[0-9a-fA-F]+|0[0-7]+
  25. escape \\t|\\n|\\r|\\f|\\\\|\\\"
  26. it_linha "\n"|"\r"|"\r\n"
  27.  
  28. %%
  29. "//"    {BEGIN COMENTARIO_LINHA; linha_aux=nlines;coluna_aux=ncolunas;ncolunas+=yyleng;}
  30. <COMENTARIO_LINHA>{it_linha}    {BEGIN 0; nlines++; ncolunas=1;if(semicolon == 1){printf("SEMICOLON\n");semicolon=0;};}
  31. <COMENTARIO_LINHA>. {ncolunas+=yyleng;}
  32.  
  33. "/*"            {BEGIN COMENTARIO;linha_aux=nlines;coluna_aux=ncolunas;ncolunas+=yyleng;}
  34. <COMENTARIO><<EOF>> {BEGIN 0;printf("Line %d, column %d: unterminated comment\n", linha_aux, coluna_aux);}
  35. <COMENTARIO>"*/"    {BEGIN 0; ncolunas+=yyleng;}
  36. <COMENTARIO>{it_linha}  {ncolunas=1;nlines++;}
  37. <COMENTARIO>.       {ncolunas+=yyleng;}
  38.  
  39. \"           {BEGIN ASPAS;linha_aux=nlines;coluna_aux=ncolunas;ncolunas+=yyleng;string =yytext;}
  40. <ASPAS>\"     {BEGIN 0;ncolunas+=yyleng;if(ver_erro == 0){printf("STRLIT(%s)\n", string);semicolon=1;};ver_erro=0;coluna_aux=0;}
  41. <ASPAS>{escape} {ncolunas+=yyleng;}
  42. <ASPAS>\\.? {printf("Line %d, column %d: invalid escape sequence (%s)\n", linha_aux, ncolunas, yytext);ver_erro=1;ncolunas+=yyleng;}
  43. <ASPAS>{it_linha}     {BEGIN 0;printf("Line %d, column %d: unterminated string literal\n", linha_aux, coluna_aux);nlines++;ver_erro=0;ncolunas=1;semicolon=0;}
  44. <ASPAS><<EOF>>  {BEGIN 0; printf("Line %d, column %d: unterminated string literal\n", linha_aux, coluna_aux);ver_erro=0; semicolon=0; return 0;}
  45. <ASPAS>.       {ncolunas+=yyleng;}
  46.  
  47.  
  48.  
  49. "package"   {printf("PACKAGE\n");ncolunas+=yyleng;semicolon=0;}
  50. ";" {printf("SEMICOLON\n");semicolon=0;ncolunas+=yyleng;}
  51. "_"     {printf("BLANKID\n");ncolunas+=yyleng;semicolon=0;}
  52. "return"      {printf("RETURN\n");semicolon=1;ncolunas+=yyleng;}
  53. "&&"        {printf("AND\n");ncolunas+=yyleng;semicolon=0;}
  54. "="     {printf("ASSIGN\n");ncolunas+=yyleng;semicolon=0;}
  55. "*"     {printf("STAR\n");ncolunas+=yyleng;semicolon=0;}
  56. ","     {printf("COMMA\n");ncolunas+=yyleng;semicolon=0;}
  57. "/"     {printf("DIV\n");ncolunas+=yyleng;semicolon=0;}
  58. "=="        {printf("EQ\n");ncolunas+=yyleng;semicolon=0;}
  59. ">"     {printf("GT\n");ncolunas+=yyleng;semicolon=0;}
  60. ">="    {printf("GE\n");ncolunas+=yyleng;semicolon=0;}
  61. "{"     {printf("LBRACE\n");semicolon=0;ncolunas+=yyleng;}
  62. "<="        {printf("LE\n");ncolunas+=yyleng;semicolon=0;}
  63. "("     {printf("LPAR\n");ncolunas+=yyleng;semicolon=0;}
  64. "["     {printf("LSQ\n");ncolunas+=yyleng;semicolon=0;}
  65. "<"     {printf("LT\n");ncolunas+=yyleng;semicolon=0;}
  66. "-"     {printf("MINUS\n");semicolon=0;ncolunas+=yyleng;}
  67. "%"     {printf("MOD\n");ncolunas+=yyleng;semicolon=0;}
  68. "!="        {printf("NE\n");ncolunas+=yyleng;semicolon=0;}
  69. "!" {printf("NOT\n");ncolunas+=yyleng;semicolon=0;}
  70. "||"        {printf("OR\n");ncolunas+=yyleng;semicolon=0;}
  71. "+"     {printf("PLUS\n");semicolon=0;ncolunas+=yyleng;}
  72. "}"     {printf("RBRACE\n");semicolon=1;ncolunas+=yyleng;}
  73. ")"     {printf("RPAR\n");semicolon=1;ncolunas+=yyleng;}
  74. "]"     {printf("RSQ\n");semicolon=1;ncolunas+=yyleng;}
  75. "else"  {printf("ELSE\n");ncolunas+=yyleng;semicolon=0;}
  76. "for"   {printf("FOR\n");ncolunas+=yyleng;semicolon=0;}
  77. "if"    {printf("IF\n");ncolunas+=yyleng;semicolon=0;}
  78. "var"   {printf("VAR\n");semicolon=0;ncolunas+=yyleng;}
  79. "int"   {printf("INT\n");semicolon=0;ncolunas+=yyleng;}
  80. "float32"   {printf("FLOAT32\n");ncolunas+=yyleng;semicolon=0;}
  81. "bool"  {printf("BOOL\n");ncolunas+=yyleng;semicolon=0;}
  82. "string"    {printf("STRING\n");ncolunas+=yyleng;semicolon=0;}
  83. "fmt.Println"   {printf("PRINT\n");ncolunas+=yyleng;semicolon=0;}
  84. "strconv.Atoi"  {printf("PARSEINT\n");ncolunas+=yyleng;semicolon=0;}
  85. "func"      {printf("FUNC\n");ncolunas+=yyleng;semicolon=0;}
  86. "os.Args"   {printf("CMDARGS\n");ncolunas+=yyleng;semicolon=0;}
  87.  
  88. {reserved}  {printf("RESERVED(%s)\n",yytext); semicolon=0;ncolunas+=yyleng;}
  89. {intlit}    {printf("INTLIT(%s)\n", yytext);semicolon=1;ncolunas+=yyleng;}
  90. {reallit}   {printf("REALLIT(%s)\n", yytext);semicolon=1;ncolunas+=yyleng;}
  91. {letra}     {printf("ID(%s)\n", yytext);semicolon=1;ncolunas+=yyleng;}
  92.  
  93.  
  94. {it_linha}  {ncolunas=1;nlines++;if(semicolon == 1 && ver_erro==0){printf("SEMICOLON\n");semicolon=0;};ver_erro=0;}
  95. <<EOF>> {return 0;}
  96. \t|\f|" "   {ncolunas+=yyleng;}
  97.  
  98. .   {printf("Line %d, column %d: illegal character (%s)\n", nlines, ncolunas, yytext); ncolunas+=yyleng;}
  99.  
  100.  
  101. %%
  102. int main()
  103. {
  104. yylex();
  105. return 0;
  106. }
  107.  
  108. int yywrap()
  109. {
  110. return 1;
  111. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top