Sunteți pe pagina 1din 18

UNIVERSIDAD TECNICA PARTICULAR DE LOJA Sistemas Informticos y Computacin

TEORIA DE AUTOMATAS Y COMPILADORES

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.

ESTRUCTURA IF if (condicin){ lnea de 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, y terminara el if con la palabra reservada endif.

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.

if ( condicin ): cdigo; else: cdigo; endif; ESTRUCTURA FOR


for ( variable = valor_inicial; variable + condicin + valor_limite ; variable + incremento_decremento) { Cdigo; }
Tambin se puede iniciar el bloque sin las llaves, en tal caso luego de las expresiones entre parntesis ira el signo dos puntos ':', y el bucle termina con la palabra reservada endfor.

for ( expresiones ) : Cdigo; endfor;

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

( ) : ; , * / ?> Endif Endfor Endswitch Break

, L, D, (, ), , L, D, (, ), : , ; , \t, \n, L L, D, , \n, \t, , L, D D, L, , L, D

-25 -26 -27 -28 -29 -30 -31 -32

; ; ; ;

-33 -34 -35 -36

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 Y SIGUIENTES GRAMATICA:


Nmero de Terminales: 35 Nmero de No Terminales: 53

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> ?>

$?> <cuerpoprog> <?php

Cocuerda (match)

$?> <cuerpoprog>

cuerpoprog <var>

$?> <var> $?> <asig> Id $?> <asig> $?> ; $?> $

Id ; ?>$ Id ; ?>$ ; ?>$ ; ?>$ ?>$ $

var id <asig> Concuerda (match)


<asig> ;

Concuerda(match)

Concuerda(match) Concuerda(match)

PILA $Programa

ENTRADA <?php Id = num ; ?>$ <?php Id = num ; ?>$ Id = num ; ?>$ Id = num ; ?>$ Id = num ; ?>$ = num ; ?>$ = num ; ?>$ num ; ?>$ ; ?>$ ?>$ $

ACCIN Programa <?php <cuerpoprog> ?>

$?> <cuerpoprog> <?php

Cocuerda (match)

$?> <cuerpoprog> $?> <var> $?> <asig> Id $?> <asig> $?>; num = $?> ; num $?> ; $?> $

cuerpoprog <var>

var id <asig> Concuerda (match)


<asig > = num ;

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){

ACCIN Programa <?php <cuerpoprog> ?>

$?> <cuerpoprog> <?php

Cocuerda (match)

$?> <cuerpoprog>

cuerpoprog <if>

$?> <if>

<if> if <if'>

$?> <if> if

Concuerda (match)

$?> <if>

<if'> ( <if''>

$?> <if><if> (

Concuerda(match)

$?> <if><if>

if'' expc <if'''>

$?> <if><if><if'''> <expc>

expc id <expc>'

$?> <if> <if> <expc> id

Concuerda(match)

$?> <if> <if> <expc>

<expc'> opr num

$?> <if> <if> <expc> num

<opr> <

<opr> $?> <if> <if> <expc> num < $?> <if> <if> <expc> num

} ?>$ < num){ } ?>$ num){ } ?>$ num){ } ?>$ ){ } ?>$ ){ } ?>$ ){ } ?>$ { } ?>$ { } ?>$ } ?>$ } ?>$ } ?>$ } ?>$ } ?>$ Concuerda(match)

Concuerda(match)

$?> <if> <if> <expc> num

Concuerda(match)

$?><if> <if> <if'''> <expc>

expc'

$?><if> <if> <if'''>

<If'''> ) <if''''>

$?><if> <if> <if'''> <if''''> )

Concuerda(match)

$?><if> <if> <if'''> <if''''>

<if''''> { <cuerpoSent> } else { <cuerpoSent> }

$?><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; ?>

S-ar putea să vă placă și