Design a new programming language grammar pdf
Here is a draft of a new programming language grammar PDF:
Language Name: Lumin
Introduction: Lumin is a modern, high-level programming language designed for ease of use, flexibility, and performance. Its syntax is inspired by popular languages such as Python, JavaScript, and C#, with a focus on simplicity, readability, and maintainability.
Syntax:
Lexical Structure
- Identifiers: A sequence of alphanumeric characters and underscores, starting with a letter or underscore. Examples:
hello
,my_variable
,foo_bar
. - Keywords: Reserved words that have special meanings in the language. Examples:
if
,else
,while
,for
,class
,function
. - Literal Values:
- Integer Literals: A sequence of digits, optionally preceded by a minus sign. Examples:
123
,-456
. - Float Literals: A sequence of digits, optionally preceded by a minus sign, and optionally followed by a decimal point and more digits. Examples:
3.14
,-0.5
. - String Literals: A sequence of characters enclosed in double quotes (
"
) or single quotes ('
). Examples:"hello"
,'hello'
. - Boolean Literals: The keywords
true
andfalse
. - Null Literal: The keyword
null
.
- Integer Literals: A sequence of digits, optionally preceded by a minus sign. Examples:
- Operators:
- Arithmetic Operators:
+
,-
,*
,/
,%
,**
(exponentiation). - Comparison Operators:
==
,!=
,<
,>
,<=
,>=
. - Logical Operators:
&&
,||
,!
. - Assignment Operators:
=
,+=
,-=
,*=
,/=
,%=
,**=
. - Bitwise Operators:
&
,|
,^
,~
.
- Arithmetic Operators:
- Separators:
- Whitespace: Spaces, tabs, and line breaks.
- Line Comments: The
//
symbol, followed by any characters until the end of the line. - Block Comments: The
/*
symbol, followed by any characters until the*/
symbol.
Syntax Elements
- Program: A sequence of statements, optionally followed by a
main
function. - Statement: A single line of code, or a block of code enclosed in curly braces (
{}
). - Expression: A sequence of operators and operands, evaluated to produce a value.
- Statement Block: A sequence of statements enclosed in curly braces (
{}
). - Function: A block of code that can be called multiple times, with optional arguments and return values.
- Class: A user-defined data type that can contain fields and methods.
Syntax Rules
- Program:
program
::=statement*
main
statement*
- Statement:
statement
::=expression
;
|statement_block
|function
|class
- Expression:
expression
::=term
(+
term
) |term
(-
term
) - Term:
term
::=factor
(*
factor
) |factor
(/
factor
) - Factor:
factor
::=literal
|identifier
|parenthesized_expression
- Parenthesized Expression:
parenthesized_expression
::=(
expression
)
- Statement Block:
statement_block
::={
statement*
}
- Function:
function
::=function_identifier
(
parameter_list
)
statement_block
- Class:
class
::=class_identifier
(
field_list
)
method_list
- Field List:
field_list
::=field
(,
field
)* - Field:
field
::=identifier
:
type
- Method List:
method_list
::=method
(,
method
)* - Method:
method
::=method_identifier
(
parameter_list
)
statement_block
Examples:
- A simple program:
print("Hello, World!");
- A function:
function greet(name) { print("Hello, " + name + "!"); }
- A class:
class Person { int age; string name; function Person(int age, string name) { this.age = age; this.name = name; } }
Error Handling:
- Syntax Errors: The language will report syntax errors using a clear and concise error message.
- Semantic Errors: The language will report semantic errors, such as undefined variables or invalid operations, using a clear and concise error message.
Future Development:
- Type System: Lumin will implement a type system to ensure that variables are used correctly and to prevent type-related errors.
- Object-Oriented Programming: Lumin will support object-oriented programming concepts, such as inheritance and polymorphism.
- Concurrency: Lumin will support concurrency features, such as threads and asynchronous programming.
This is just a draft, and the language is still under development. I hope this gives you an idea of what the grammar of Lumin might look like. Let me know if you have any questions or if you'd like to contribute to the development of Lumin!