Advertisement
Guest User

trab1

a guest
Aug 30th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Latex 8.43 KB | None | 0 0
  1. \documentclass[a4paper,10pt]{article}
  2. \usepackage[utf8]{inputenc}
  3.  
  4. %opening
  5. \title{Trabalho de Compiladores - Analisador Léxico}
  6. \author{Leonardo Ferreira e Mateus Tassinari}
  7.  
  8. \begin{document}
  9.  
  10. \maketitle
  11.  
  12. \begin{abstract}
  13. Documentação de nosso analisador léxico, que será utilizado futuramente por um interpretador da linguagem G-Portugol.
  14. \end{abstract}
  15.  
  16. \section{Introdução}
  17. Análise léxica, de um modo geral, é o processo de analisar todos os caracteres de um determinado código e produzir uma sequência de símbolos chamados de ``tokens''.
  18. Um ``token'' descreve um padrão de caracteres que terá algum significado específico, de acordo com a linguagem que será interpretada, podendo ser identificadores, operadores, caracteres, números, atribuições ou palavras reservadas daquela linguagem.
  19.  
  20. Em nosso trabalho, foi desenvolvido um analisador léxico para a linguagem G-Portugol através da ferramenta Flex. Com o programa Flex nosso trabalho é poupado, de maneira que só precisamos descrever expressões regulares em blocos de código e ele se encarregará de gerar um analisador léxico para estas expressões na linguagem C.  
  21. \section{Classificação dos caracteres}
  22.  \subsection{Palavras Chave}
  23.  A linguagem G-Portugol define as seguintes palavras chave ou reservadas:
  24.  \begin{verbatim}
  25.  fim-variáveis, algoritmo, variáveis, inteiro, real,
  26.  caractere, literal, lógico, início, verdadeiro,
  27.  falso, fim, ou, e, não, se, senão, então, fim-se,
  28.  faça, fim-enquanto, para, de, até, enquanto,
  29.  fim-para, matriz, inteiros, reais, caracteres,
  30.  literais, lógicos, função, retorne, passo.
  31.  \end{verbatim}
  32.  
  33.  E o bloco de códigos correspodente a elas:
  34.  \begin{verbatim}
  35. fim-variaveis   {printf("%s: PALAVRA RESERVADA\n", yytext);}
  36. caractere   {printf("%s: PALAVRA RESERVADA\n", yytext);}   
  37. se      {printf("%s: PALAVRA RESERVADA\n", yytext);}
  38. faca        {printf("%s: PALAVRA RESERVADA\n", yytext);}
  39. fim-para    {printf("%s: PALAVRA RESERVADA\n", yytext);}
  40. literais    {printf("%s: PALAVRA RESERVADA\n", yytext);}
  41. algoritmo   {printf("%s: PALAVRA RESERVADA\n", yytext);}
  42. literal     {printf("%s: PALAVRA RESERVADA\n", yytext);}
  43. fim     {printf("%s: PALAVRA RESERVADA\n", yytext);}
  44. senao       {printf("%s: PALAVRA RESERVADA\n", yytext);}
  45. fim-enquanto    {printf("%s: PALAVRA RESERVADA\n", yytext);}
  46. matriz      {printf("%s: PALAVRA RESERVADA\n", yytext);}
  47. logicos     {printf("%s: PALAVRA RESERVADA\n", yytext);}
  48. variaveis   {printf("%s: PALAVRA RESERVADA\n", yytext);}
  49. logico      {printf("%s: PALAVRA RESERVADA\n", yytext);}
  50. ou      {printf("%s: PALAVRA RESERVADA\n", yytext);}
  51. entao       {printf("%s: PALAVRA RESERVADA\n", yytext);}
  52. para        {printf("%s: PALAVRA RESERVADA\n", yytext);}
  53. inteiros    {printf("%s: PALAVRA RESERVADA\n", yytext);}
  54. funcao      {printf("%s: PALAVRA RESERVADA\n", yytext);}
  55. inteiro     {printf("%s: PALAVRA RESERVADA\n", yytext);}
  56. inicio      {printf("%s: PALAVRA RESERVADA\n", yytext);}
  57. e       {printf("%s: PALAVRA RESERVADA\n", yytext);}
  58. fim-se      {printf("%s: PALAVRA RESERVADA\n", yytext);}
  59. de      {printf("%s: PALAVRA RESERVADA\n", yytext);}
  60. reais       {printf("%s: PALAVRA RESERVADA\n", yytext);}
  61. retorne     {printf("%s: PALAVRA RESERVADA\n", yytext);}
  62. real        {printf("%s: PALAVRA RESERVADA\n", yytext);}
  63. nao     {printf("%s: PALAVRA RESERVADA\n", yytext);}
  64. enquanto    {printf("%s: PALAVRA RESERVADA\n", yytext);}
  65. ate     {printf("%s: PALAVRA RESERVADA\n", yytext);}
  66. caracteres  {printf("%s: PALAVRA RESERVADA\n", yytext);}
  67. passo       {printf("%s: PALAVRA RESERVADA\n", yytext);}
  68.  
  69.   \end{verbatim}
  70. As palavras reservadas ``verdadeiro'' e ``falso'' foram classificadas como valores lógicos em nosso analisador.
  71.  \subsection{Identificadores}
  72.  Segundo o próprio manual da linguagem G-Portugol, todos seus identificadores (nomes de variáveis, funções, etc) são identificados sempre por começo contendo letras (maiúsculas ou minúsculas) ou underscore, seguidos de letras, números ou underscore, zero ou mais vezes.
  73.  \begin{verbatim}
  74.  
  75. [a-zA-Z_][a-zA-Z0-9_]{ printf("%s -> IDENTIFICADOR\n",yytext);}
  76.   \end{verbatim}
  77.  
  78.  
  79. \subsection{Inteiros e Floats}
  80.  Números inteiros são definidos como uma sequência de dígitos seguidos, opcionalmente, de um expoente positivo. Para reconhecê-los,
  81.  definimos:
  82.  \begin{verbatim}
  83. digitos [0-9]+
  84. expoente_positivo [eE]{digitos}
  85. inteiro {digitos}+{expoente_positivo}?
  86. \end{verbatim}
  87.  
  88. Floats são definidos como uma sequência de dígitos, seguidos de um ponto, outra sequência de dígitos e um expoente. Floats foram
  89. reconhecidos como:
  90. \begin{verbatim}
  91. expoente [eE]-?{digitos}
  92. fracao "."{digitos}
  93. float {digitos}{fracao}?{expoente}?
  94. \end{verbatim}  
  95.  
  96.  
  97.  \subsection{Operadores aritméticos}
  98.  Os operadores aritméticos são (+  -  *  /  ++  -- \% $\ll$ $\gg$). O trecho de código correspondente a eles é:
  99.  \begin{verbatim}
  100. "+" {printf("%s -> OPERADORARITMETICO\n",yytext);}
  101. "-" {printf("%s -> OPERADORARITMETICO\n",yytext);}
  102. "*" {printf("%s -> OPERADORARITMETICO\n",yytext);}
  103. "/" {printf("%s -> OPERADORARITMETICO\n",yytext);}
  104. "%" {printf("%s -> OPERADORARITMETICO\n",yytext);}
  105. "++" {printf("%s -> OPERADORARITMETICO\n",yytext);}
  106. "--" {printf("%s -> OPERADORARITMETICO\n",yytext);}
  107. ">>" {printf("%s -> OPERADORARITMETICO\n",yytext);}
  108. "<<" {printf("%s -> OPERADORARITMETICO\n",yytext);}
  109.  
  110.   \end{verbatim}
  111.  
  112.  
  113.  \subsection{Operadores Relacionais}
  114.  Os operadores relacionais são ($>$, $>=$, $<$, $<=$, $==$, $!=$). O trecho de código correspondente a eles é:
  115.  \begin{verbatim}
  116. ">" {printf("%s -> OPERADORRELACIONAL\n",yytext);}
  117. "<" {printf("%s -> OPERADORRELACIONAL\n",yytext);}
  118. ">=" {printf("%s -> OPERADORRELACIONAL\n",yytext);}
  119. "<=" {printf("%s -> OPERADORRELACIONAL\n",yytext);}
  120. "==" {printf("%s -> OPERADORRELACIONAL\n",yytext);}
  121. "!=" {printf("%s -> OPERADORRELACIONAL\n",yytext);}
  122.   \end{verbatim}
  123.  
  124.  \subsection{Operadores Lógicos}
  125.  Os operadores lógicos são (\&\& $||$ ! \^). O trecho de código correspondente a eles é:
  126.  \begin{verbatim}
  127. "&&" {printf("%s -> OPERADORLOGICO\n",yytext);}
  128. "||" {printf("%s -> OPERADORLOGICO\n",yytext);}
  129. "&" {printf("%s -> OPERADORLOGICO\n",yytext);}
  130. "|" {printf("%s -> OPERADORLOGICO\n",yytext);}
  131. "^" {printf("%s -> OPERADORLOGICO\n",yytext);}
  132. "!" {printf("%s -> OPERADORLOGICO\n",yytext);}
  133.   \end{verbatim}
  134.  
  135.  \subsection{Símbolos Especiais e Operadores de Atribuição}
  136.  Os símbolos especiais são ( ( ) , ; : \{ \} \# ' $\backslash$ \textquotedblleft . ). O trecho de código correspondente a eles e aos operadores de atribuição é:
  137.  \begin{verbatim}
  138. "(" {printf("%s -> SIMBOLOESPECIAL\n",yytext);}
  139. ")" {printf("%s -> SIMBOLOESPECIAL\n",yytext);}
  140. "," {printf("%s -> SIMBOLOESPECIAL\n",yytext);}
  141. ";" {printf("%s -> SIMBOLOESPECIAL\n",yytext);}
  142. ":" {printf("%s -> SIMBOLOESPECIAL\n",yytext);}
  143. "{" {printf("%s -> SIMBOLOESPECIAL\n",yytext);}
  144. "}" {printf("%s -> SIMBOLOESPECIAL\n",yytext);}
  145. "#" {printf("%s -> SIMBOLOESPECIAL\n",yytext);}
  146. "'" {printf("%s -> SIMBOLOESPECIAL\n",yytext);}
  147. "\"" {printf("%s -> SIMBOLOESPECIAL\n",yytext);}
  148. "\\" {printf("%s -> SIMBOLOESPECIAL\n",yytext);}
  149. "." {printf("%s -> SIMBOLOESPECIAL\n",yytext);}
  150. "=" {printf("%s -> ATRIBUICAO\n",yytext);}
  151. "+=" {printf("%s -> ATRIBUICAO\n",yytext);}
  152. "-=" {printf("%s -> ATRIBUICAO\n",yytext);}
  153. "*=" {printf("%s -> ATRIBUICAO\n",yytext);}
  154. "/=" {printf("%s -> ATRIBUICAO\n",yytext);}
  155. "&=" {printf("%s -> ATRIBUICAO\n",yytext);}
  156. "^=" {printf("%s -> ATRIBUICAO\n",yytext);}
  157. "|=" {printf("%s -> ATRIBUICAO\n",yytext);}
  158. ">>=" {printf("%s -> ATRIBUICAO\n",yytext);}
  159. "<<=" {printf("%s -> ATRIBUICAO\n",yytext);}
  160.  
  161.   \end{verbatim}
  162.  
  163. \subsection{Comentários}
  164.  Comentários devem ser ignorados pelo analisador léxico. Comentário de uma única linha são simples, porém os comentários em bloco
  165.  são um pouco mais complexos. O código utilizado para reconhecê-los foi:
  166.  \begin{verbatim}
  167. comentario_linha "//"[^\n]*
  168. comentario_bloco "/""*"+([^*/][^*]*"*"+)*"/"
  169. \end{verbatim}
  170. Além disso, quando encontra-se um bloco de comentário, é feita uma pequena correção na variável lines, que conta o número de linhas
  171. do arquivo, para incluir as quebras de linha que estejam dentro do bloco de comentário:
  172. \begin{verbatim}
  173. {comentario_bloco} {
  174.   strcpy(comment,yytext);  
  175.  for(i=0; i<strlen(comment); i++) {
  176.    if(comment[i] == '\n') lines++;
  177.  }
  178. }
  179. \end{verbatim}
  180.  
  181. \section{Exemplos}
  182. Nos exemplos, buscamos identificar os diversos operadores, identificadores e demais características do input. Também prestamos atenção
  183. aos blocos de comentários que foram problemáticos.
  184. \end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement