Beruflich Dokumente
Kultur Dokumente
PROYECTO
Analizado r lxico y si ntctico para el Analizad or lxico y si ntctico para el lenguaje P HP lenguaje P HP AUTORES:
Pablo Torres Myriam Sarango Fabin Yuquilema
SUPERVISOR:
Ing. Mara Beln Mora
CICLO:
Sptimo Ciclo
CICLO ACADEMICO:
Octubre 2010 Febrero 2011
OBJETIVOS:
General: Especficos: Comprender y analizar la sintaxis del lenguaje PHP. Desarrollar un autmata. Desarrollar un analizador lxico. Desarrollar un analizador semntico. Aplicar los conocimientos obtenidos en la materia de Autmatas y Compiladores.
INTRODUCCIN:
El presente proyecto est dirigido a la construccin de un analizador lxico, as como tambin del semntico para el lenguaje PHP, los cuales realizan el reconocimiento de palabras, signos y letras que son permitidos dentro del cdigo del lenguaje, si se encuentra algo distinto devuelve un error y nos muestra el origen del error. Para los cual utilizaremos los conocimientos obtenidos en la materia de TEORIA DE AUTOMATAS Y COMPILADORES.
DESCRIPCIN:
Lenguaje en el que se va desarrollar el proyecto: Java (NetBeans) Lenguaje del compilador: PHP.
IDENTIFICADORES:
Alfabeto = { a, b, c, d, e, f, g, h, i, j, , l, m, n, o, p, q, r, s, t, u, v, w, x, y, z } Dgitos = { 1,2,3,4,5,6,7,8,9,0 } Delimitadores = { <?php , ?>, [, ], {, }, (, ), , , ; } Operadores Aritmticos = { =, <, >, +, , * , /, } Comentarios = { // } Operadores lgicos = { and, or}
Palabras Reservadas:
for switch break endwhile endfor while define endif If Do Print endswitch else Case Echo Or and
<?php ?>
DEFINICIN DE ESTRUCTURAS:
ESTRUCTURA PHP Declaracin: <?php //codigo php. ?> ESTRUCTURA VARIABLES
En php no se declaran los tipos de datos, estos son asumidos dependiendo del valor que tenga la variable. Todas las variables deben iniciar con el signo $, para poder ser identificadas como variables.
Declaracin: $ + nombre_variable + = + valor + ; ESTRUCTURA CONSTANTES: Declaracin: define(nombre_variable, valor); ESTRUCTURA PARA SALIDAS DE PANTALLA Declaracin: echo texto a mostrar; print texto a mostrar;
Se debe tener en cuenta que el separador es un espacio, y no la comilla, tanto luego de la palabra reservada echo, como de la palabra print.
if (condicin): lnea de cdigo; endif; ESTRUCTURA IF ELSE if ( condicin ){ cdigo; } else { cdigo; }
Tambin se puede iniciar el bloque sin las llaves, en tal caso luego de la condicin entre parntesis ira el signo dos puntos ":" luego las sentencias, despus el else con el signo dos puntos ":", nuevamente las sentencias y terminara con la palabra reservada endif.
ESTRUCTURA WHILE while (variable a evaluar + carcter lgico+ valor con el que evaluara ){ Cdigo; }
Tambin se puede iniciar el bloque sin las llaves, se escribe el signo dos puntos ':' luego de la condicin entre parntesis, y se finaliza el bloque con la palabra reservada endwhile.
while (variable a evaluar + carcter lgico+ valor con el que evaluara ): Cdigo; endwhile; ESTRUCTURA DO WHILE do { Cdigo; } while (variable a evaluar + condicin lgica + valor con el que evaluara); ESTRUCTURA SWITCH switch + ( + valor_a_avaluar + ) + { case + alternativa_1 + ; codig; break; case + alternativa_2 + ; codig; break; case + alternativa_n + ; codig; break; }
En este caso tambin en vez del signo { puede ir el signo dos puntos ':' luego de la condicin entre parntesis, y la palabra reservada endswitch al final.
TABLA DE SEPARADORES: Palabras Reservadas <?php Case Define do Echo Else For if Print Switch While Endwhile Or And D(digitos) L(letras) + = < > { } \n, , \t, \r , ( , \t, \n , { , \t, \n, \r, { , : , ( , ( , ( , ( ; , \t , \t , =, coma, (, ;, +, - , /, *, or, and , D, ;, (, =, +, -, *, /, and, or , ), L, D, = , ; , ;, L, D , ;, L, D , L, D , L, D , L, D , L, D, \t, \n , L, D, \t, \n Separadores Token -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20 -21 -22 -23 -24
; ; ; ;
AUTOMATA DETERMINISTA:
DEFINICION DE GRAMATICA:
SENTENCIA PROGRAMA:
programa <?php <cuerpoprog> ?>
Cuerpo programa:
<cuerpoprog> <for> | <while> | <do-while> | <switch> | < if> | <echo> | <print> | <define > |<variable> Variable: <var> $ <id> <asig> <asig> ; | = <num> ; Operadores relacionales: <opr> < | > |= Operadores aritmticos: <opa> +| - | * | / Operadores lgicos: <opl> or | and Expresin Condicional: <expc> id <opr> num | num <opr> id |id <opl> id
Cuerpo Sentencias:
<cuerpoSent> id = id <opa> id ; |id = id <opa> num ; | id = num <opa> id ;
For:
<for> for (id=mun; id <opr>id; id<opa>){ <cuerpoSent> } | for (id=mun; id <opr>id; id<opa>); <cuerpoSent> endfor;
While:
<while> while(expc){ <cuerpoSent>} | while(expc): <cuerpoSent> endwhile;
Do-While:
<Dowhile> do{ <cuerpoSent> } while(expc) | do: <cuerpoSent> while(expc) endwhile;
If:
<if> if(expc){ <cuerpoSent> } else{<cuerpoif>} | if(expc): <cuerpoSent> endif; else: <cuerpoSent> endif;
Switch:
<switch> switch (id){ <cuerposwitch>} | switch (id): <cuerposwitch> endswitch; <cuerposwitch> case(num); <cuerpoSent>
Echo:
<echo> echo <cuerpoecho> <cuerpoecho> id | num
Print:
<print> print <cuerpoprint> <cuerpoprint> id | num
Define:
<define> define (id,num)|define(id,id)
GRAMATICA FACTORIZADA:
programa <?php <cuerpoprog> ?> <cuerpoprog> <for> | <while> | <dowhile> | <switch> | <if> | <echo> | <print> | <define> | <var> <opr> < | > | = <opa> + | - | * | / <opl> or | and <expc> num <opr> id | id <expc'> <cuerpoSent> id <cuerpoSent'> <for> for <for'> <while> while <while'> <dowhile> do <dowhile'> <if> if <if>' <switch> switch <switch'> <cuerposwitch> case ( num ) ; <cuerpoSent> <echo> echo <cuerpoecho> ; <cuerpoecho> id | num <print> print <cuerpoprint> ; <cuerpoprint> id | num <define> define <define'> ; <expc'> opr num | opl id <cuerpoSent'> = <cuerpoSent'' > <for'> ( <for''> <while'> ( <while''> <dowhile'> { cuerpoSent } while' ( <expc >) | : cuerpoSent while ( <expc >) endwhile ; <if'> ( <if''> <switch'> ( <switch''> <define'> ( <define'' > cuerpoSent'' num opa id ; | id cuerpoSent''' <for''> id <for'''> <while''> expc <while'''> <if''> <expc> <if'''> <switch''> id <switch'''> <define''> id <define'''> <cuerpoSent'''> opa <cuerpoSent'''' > <for'''> = <for''''> <while'''> ) <while''''> <If'''> ) <if''''> <switch'''> ) <switch''''> <define'''> , <define''''> <cuerpoSent''''> id : | num ; <for''''> num <for'''''> <while''''> { <cuerpoSent> } | : <cuerpoSent> endwhile ; <if''''> { <cuerpoSent> } else { <cuerpoSent> } | : <cuerpoSent> endif ; else : <cuerpoSent> endif ;
<switch''''> { <cuerposwitch> } | : <cuerposwitch> endswitch; <define'''' > num ) | id ) <for'''''> ; <for''''''> <for''''''> id <for'''''''> <for'''''''> opr <for''''''''> <for''''''''> id <for'''''''''> <for'''''''''> ; <for''''''''''> <for''''''''''> id <for'''''''''''> <for'''''''''''> opa <for''''''''''''> <for''''''''''''> ) <for'''''''''''''> <for'''''''''''''> { <cuerpoSent> } | : <cuerpoSent> endfor ; <var> <id> <asig> <asig> ; | = <num> ;
PRIMEROS
programa { <?php } cuerpoprog { for, while, switch, if, echo, print, define, do, id } opr { <, >, = }
SIGUIENTES
programa { $ } cuerpoprog { ?> } opr { id, num } opa {id, num, ) }
opa { +, -, *, / } opl { id } opl { or, and } expc { ) } expc { num, id } cuerpoSent { id } for { for } while { while } dowhile { do } if { if } switch { switch } cuerposwitch { case } echo { echo } cuerpoSent { }, while, endwhile, endif, endfor, endswitch } for { ?> ( } while { ?> ( } dowhile { ?> } if { ?> ( } switch { ?> ( } cuerposwitch { }, endswitch } echo { ?>, id, num }
cuerpoecho { id, num } print { print } cuerpoprint { id, num } define { define } expc' { < >, =, or, and } cuerpoSent' { = } for' { ( }
cuerpoecho { ?>, id, num } print { ?>, id, num } cuerpoprint { ?>, id, num } define { ?>, ( } expc' { ) } cuerpoSent' { }, while, endwhile, endif, endfor, endswitch } for' { ?>, ( }
while' { ( } while' { ?>, (, } dowhile' { {, : } dowhile' { ?> } if' { ( } if' { ?>, ( } switch' { ( } switch' { ?>, ( } define' { ( } define' { ?>, ( } cuerpoSent'' { num, id } for'' { id } while'' { num id } if'' { num, id } switch'' { id } define'' { id } cuerpoSent''' { +, -, *, / } for''' { = } while''' { ) } for''' { ?>, ( } if''' { ) } while''' { ?>, ( } switch''' { ) } if''' { ?>, ( } define''' { , } switch''' { ?>, ( } cuerpoSent'''' { id, num } define''' { ?>, ( } for'''' { num } cuerpoSent'' { }, while, endwhile, endif, endfor, endswitch } for'' { ?>, ( } while'' { ?>, ( } if'' { ?>, ( } switch'' { ?>, ( } define'' { ?>, ( } cuerpoSent''' { }, while, endwhile, endif endfor, endswitch }
while'''' { { : } if'''' { { : } switch'''' { { : } define'''' { num, id } for''''' { ; } for'''''' { id } for''''''' { <, >, = } for'''''''' { id } for''''''''' { ; } for'''''''''' { id } for''''''''''' { +, -, *, / } for'''''''''''' - { ) } for''''''''''''' { {, : } var { id } asig { ; , =}
cuerpoSent'''' { }, while, endwhile, endif endfor, endswitch } for'''' { ?>, ( } while'''' { ?>, ( } if'''' { ?>, ( } switch'''' { ?>, ( } define'''' { ?>, ( } for''''' { ?>, ( } for'''''' { ?>, ( } for''''''' { ?>, ( } for'''''''' { ?>, ( } for''''''''' { ?>, ( } for'''''''''' { ?>, ( } for''''''''''' { ?>, ( } for'''''''''''' { ?>, ( } for''''''''''''' { ?>, (} var { ?> } asig { ?> }
EJEMPLOS VALIDOS:
PILA $Programa ENTRADA <?php Id ; ?>$ <?php Id ; ?>$ Id ; ?>$ ACCIN Programa <?php <cuerpoprog> ?>
Cocuerda (match)
$?> <cuerpoprog>
cuerpoprog <var>
Concuerda(match)
Concuerda(match) Concuerda(match)
PILA $Programa
ENTRADA <?php Id = num ; ?>$ <?php Id = num ; ?>$ Id = num ; ?>$ Id = num ; ?>$ Id = num ; ?>$ = num ; ?>$ = num ; ?>$ num ; ?>$ ; ?>$ ?>$ $
Cocuerda (match)
$?> <cuerpoprog> $?> <var> $?> <asig> Id $?> <asig> $?>; num = $?> ; num $?> ; $?> $
cuerpoprog <var>
Concuerda(match)
Concuerda(match)
Concuerda(match)
Concuerda(match) Concuerda(match)
PILA $Programa
ENTRADA <?php If ( id < num){ } ?>$ <?php If ( id < num){ } ?>$ If ( id < num){ } ?>$ If ( id < num){ } ?>$ If ( id < num){ } ?>$ ( id < num){ } ?>$ ( id < num){ } ?>$ id < num){ } ?>$ id < num){ } ?>$ id < num){ } ?>$ < num){ } ?>$ < num){
Cocuerda (match)
$?> <cuerpoprog>
cuerpoprog <if>
$?> <if>
<if> if <if'>
$?> <if> if
Concuerda (match)
$?> <if>
<if'> ( <if''>
$?> <if><if> (
Concuerda(match)
$?> <if><if>
expc id <expc>'
Concuerda(match)
<opr> <
<opr> $?> <if> <if> <expc> num < $?> <if> <if> <expc> num
} ?>$ < num){ } ?>$ num){ } ?>$ num){ } ?>$ ){ } ?>$ ){ } ?>$ ){ } ?>$ { } ?>$ { } ?>$ } ?>$ } ?>$ } ?>$ } ?>$ } ?>$ Concuerda(match)
Concuerda(match)
Concuerda(match)
expc'
<If'''> ) <if''''>
Concuerda(match)
$?><if> <if> <if'''> <if''''> } <cuerpoSent> { else } <cuerpoSent> { $?><if> <if> <if'''> <if''''> } <cuerpoSent> { else } <cuerpoSent> $?><if> <if> <if'''> <if''''> } <cuerpoSent> { else } $?><if> <if> <if'''> <if''''> } <cuerpoSent> { else $?><if> <if> <if'''> <if''''> } <cuerpoSent> $?><if> <if> <if'''> <if''''> }
Concuerda(match)
cuerpoSent
Concuerda(match)
Else
cuerpoSent
Concuerda(match)
$?><if> <if> <if'''> <if''''> $?><if> <if> <if'''> $?><if> <if> $?><if> $?> $
If If If If Concuerda(match) Concuerda(match)
EJEMPLOS NO VALIDOS:
If (num < id){ } <?php if ( num < id ){ } ?> <?php Echo id; ?>