Lexical Conventions in Lua Programming


In this article, we will learn how to declare and write different lexical conventions in Lua programming.

In Lua, we call NAMES as IDENTIFIERS and they can be any string of letters, digits, and underscored, and they should not begin with a digit.

Let’s consider an example of different Identifiers in Lua and see which ones are valid and which aren’t.

Example

Consider the example shown below −

 Live Demo

i = 10
print(i)

j1 = 11
print(j1)

_ij = 99
print(_ij)

aVeryLongName = "Tutorials point"
print(aVeryLongName)

In the above example, all the variables (Identifiers) are valid, as they either start with letters, digits, or with an underscore.

Output

10
11
99
Tutorials point

Now, let’s consider an example where the identifiers that we declare are not valid according to Lua, and should lead to an error.

Example

Consider the example shown below −

10jzz = 11
print(10jzz)

$z = 1
print($z)

Output

input:1: malformed number near '10j'

The program stopped on line 1 as it encountered an error, resulting in the termination of the program.

Now, let’s talk about reserved words (i.e., keywords) in Lua. There are many different keywords in Lua. Here's the list −

andbreakdoelseelseifend
falseforfunctionifinlocal
nilnotorrepeatreturnthen
trueuntilwhile


All the above words can be used as per their use-cases, but we cannot use them in place of identifiers such as the name of a variable.

Example

Consider the example shown below −

 Live Demo

do = 1
print(do)

Output

input:1: unexpected symbol near '='

Another important point to note about lexical conventions in Lua is that the Lua programming language is case-sensitive, which means that "and" and "AND" are two different identifiers.

Example

Consider the example shown below −

 Live Demo

AND = 1
print(AND)

x = 2 and 3
print(x)

Output

1
3

Updated on: 19-Jul-2021

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements