2011 LUA: Gramática
Antonio Acosta Acosta Murillo Murillo Instituto Tecnológico de Culiacán 03/09/2011
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Programación de Sistemas Antes de comenzar comenzar a desarrollar desarrollar la gramática del del lenguaje de de programación programación Lua, haré haré una breve introducción para conocer un poco sobre las bondades de este lenguaje de programación que rápidamente está escalando posiciones en el ranking de los lenguajes de programación según la encuesta de la página web Tiobe 1. Lua es un lenguaje de programación imperativa, estructurada y bastante ligero que fue diseñado como un lenguaje interpretado con una semántica que se pudiera extender. El nombre significa "luna" en portugués y gallego. Lua fue creado en 1993 por Roberto Ierusalimschy, Luiz Henrique de Figueiredo y Waldemar Celes, miembros del Grupo de Tecnología en Computación Gráfica (Tecgraf) en la Pontificia Universidad Católica de Río de Janeiro. Las versiones de Lua anteriores a la 5.0 fueron distribuidas bajo una licencia similar a la BSD, de la versión 5.0 en adelante se utiliza la licencia MIT, compatible con la GPL. Lua ha sido usado en muchas aplicaciones comerciales y no comerciales, cuyo número incrementa cada año. Lua es un lenguaje de extensión, suficientemente compacto para usarse en diferentes plataformas. En lua las variables no tienen tipo, sólo los datos y pueden ser lógicos, enteros, números de coma flotante o cadenas. Estructuras de datos como vectores, conjuntos, tablas hash, listas y registros pueden ser representadas utilizando la única estructura de datos de Lua: la tabla.
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Programación de Sistemas
Lua 5.1 Gramática %fallback OPEN '(' . chunk
::= block .
semi semi
::= ';' . ::= .
block block ublock
::= scope statlist . ::= scope statlist laststat semi . ::= block 'until' exp .
scope scope
::= . ::= scope statlist binding semi.
statlist ::= . statlist ::= statlist stat semi . stat stat stat stat stat stat stat stat
::= 'do' block 'end' . ::= 'while' exp 'do' block 'end' . ::= repetition 'do' block 'end' . ::= 'repeat' ublock . ::= 'if' conds 'end' . ::= 'function' funcname funcbody . ::= setlist '=' explist1 . ::= functioncall .
repetition ::= 'for' NAME '=' explist23 . repetition ::= 'for' namelist 'in' explist1 . conds conds condlist condlist cond
::= condlist . ::= condlist 'else' block . ::= cond . ::= condlist 'elseif' cond . ::= exp 'then' block .
laststat ::= 'break' . laststat ::= 'return' . laststat ::= 'return' explist1 .
%right %left %left %right %right
'..' . '+' '-' . '*' '/' '%' . 'not' '#' . '^' .
exp exp exp exp exp exp exp exp exp exp exp exp
::= 'nil'|'true'|'false'|NUMBER|STRING|' 'nil'|'true'|'false'|NUMBER|STRING|'...' ...' . ::= function . ::= prefixexp . ::= tableconstructor . ::= 'not'|'#'|'-' exp . ['not'] ::= exp 'or' exp . ::= exp 'and' exp . ::= exp '<'|'<='|'>'|'>='|'=='|'~=' '<'|'<='|'>'|'>='|'=='|'~=' exp . ::= exp '..' exp . ::= exp '+'|'-' exp . ::= exp '*'|'/'|'%' exp . ::= exp '^' exp .
setlist setlist
::= var . ::= setlist ',' var .
var var var
::= NAME . ::= prefixexp '[' exp ']' . ::= prefixexp '.' NAME .
prefixexp ::= var var . prefixexp ::= functioncall functioncall . prefixexp ::= OPEN exp ')' . functioncall ::= prefixexp args . functioncall ::= prefixexp ':' NAME args . args args args args
::= '(' ')' . ::= '(' explist1 ')' . ::= tableconstructor tableconstructor . ::= STRING .
function binding binding binding
::= 'local' namelist . ::= 'local' namelist '=' explist1 . ::= 'local' 'function' NAME funcbody .
funcname ::= dottedname . funcname ::= dottedname dottedname ':' NAME NAME . dottedname ::= NAME . dottedname ::= dottedname '.' NAME . namelist ::= NAME . namelist ::= namelist namelist ',' NAME . explist1 ::= exp . explist1 ::= explist1 ',' exp exp . explist23 ::= exp ',' exp . explist23 ::= exp ',' exp exp ',' exp . %left %left %left
'or' . 'and' . '<' '<=' '>' '>=' '==' '~='
::= 'function' funcbody .
funcbody
::= params block 'end' .
params
::= '(' parlist ')' .
parlist parlist parlist parlist
::= . ::= namelist . ::= '...' . ::= namelist ',' '...' .
tableconstructor ::= '{' '}' . tableconstructor ::= '{' fieldlist '}' . tableconstructor ::= '{' fieldlist ','|';' '}' . fieldlist ::= field . fieldlist ::= fieldlist ','|';' field . field field field
::= exp . ::= NAME '=' exp . ::= '[' exp ']' '=' exp .
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Programación de Sistemas
Lua Sintaxis chunk ::= { sentencia [';']} [última_sentencia[';']] bloque ::= chunk sentencia ::= varlist '=' explist | llamada_a_func | do bloque end | while exp do bloque end | repeat bloque until exp exp | if exp then bloque bloque {elseif exp then bloque} [else [else bloque] end | for nombre '=' exp exp ',' exp [',' [',' exp] do bloque end | for lista_de_nombres lista_de_nombres in explist do bloque end | function nombre_de_func cuerpo_de_func | local function nombre cuerpo_de_func | local lista_de_nombres ['=' explist] última_sentencia ::= return [explist] | break nombre_de_func ::= nombre {'.' nombre} [':' nombre] varlist ::= var {',' var} var ::= nombre | prefixexp '[' exp ']' | prefixexp '.' nombre lista_de_nombres ::= nombre {',' nombre} explist ::= {exp ','} exp exp ::= nil | false | true | número | string | '...' | func | prefixexp | constructor_de_tabla | exp operador_binario exp | operador_unario exp prefixexp ::= var | llamada_a_func | '(' exp ')' llamada_a_func ::= prefixexp arg_actuales | prefixexp ':' nombre args_actuales args_actuales args_actuales ::= '(' [explist] ')' | constructor_de_tabla | string func ::= function cuerpo_de_func cuerpo_de_func ::= '(' [args_formal_list] ')' bloque end args_formal_list ::= lista_de_nombres lista_de_nombres [',' '...'] '...'] | '...' constructor_de_tabla ::= '{' [lista_de_campos] '}' lista_de_campos ::= campo {separador_de_campo campo} [separador_de_campo] campo ::= ::= '[' '[' exp exp ']' '=' exp | nombre '=' exp exp | exp separador_de_campo ::= ',' | ';'
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Programación de Sistemas
Ejemplos de programas en Lua --Wrap a string at a given margin This is intended for strings without newlines in them (i.e. after reflowing the text and breaking it into paragraphs.) function wrap(str, limit, indent, indent1) indent = indent or "" indent1 = indent1 or indent limit = limit or 72 local here = 1-#indent1 return indent1..str:gsub("(%s indent1..str:gsub("(%s+)()(%S+)()", +)()(%S+)()", function(sp, st, word, fi) if fi-here > limit then here = st - #indent return "\n"..indent..word end end) end
-- Bisect.lua -- bisection method for solving non-linear equations delta=1e-6
-- tolerance
function bisect(f,a,b,fa,fb) local c=(a+b)/2 io.write(n," c=",c," a=",a," b=",b,"\n") if c==a or c==b or math.abs(a-b)
Trusted by over 1 million members
Try Scribd FREE for 30 days to access over 125 million titles without ads or interruptions! Start Free Trial Cancel Anytime.
Programación de Sistemas
Output Bisect.lua 0 c=1.5 a=1 b=2 1 c=1.25 a=1 b=1.5 2 c=1.375 a=1.25 b=1.5 3 c=1.3125 a=1.25 b=1.375 4 c=1.34375 a=1.3125 b=1.375 5 c=1.328125 a=1.3125 b=1.34375 6 c=1.3203125 a=1.3125 b=1.328125 7 c=1.32421875 a=1.3203125 b=1.328125 8 c=1.326171875 a=1.32421875 b=1.328125 9 c=1.3251953125 a=1.32421875 b=1.326171875 10 c=1.32470703125 a=1.32421875 b=1.3251953125 11 c=1.324951171875 a=1.32470703125 b=1.3251953125 12 c=1.3248291015625 a=1.32470703125 b=1.324951171875 13 c=1.3247680664062 a=1.32470703125 b=1.3248291015625 14 c=1.3247375488281 a=1.32470703125 b=1.3247680664062 15 c=1.3247222900391 a=1.32470703125 b=1.3247375488281 16 c=1.3247146606445 a=1.32470703125 b=1.3247222900391 17 c=1.3247184753418 a=1.3247146606445 b=1.3247222900391 18 c=1.3247165679932 a=1.3247146606445 b=1.3247184753418 19 c=1.3247175216675 a=1.3247165679932 b=1.3247184753418 20 c=1.3247179985046 a=1.3247175216675 b=1.3247184753418 after 20 steps, root is 1.3247179985046387 with error 9.5e-07, f=1.8e-07