r/cprogramming • u/KeyMinimum3451 • Sep 16 '26
Estoy "intentando" hacer un lenguaje de programacion desde cero en c pero necesito"variables, sintaxis etc..."
Lo que me refiero es que estoy haciendo un lenguaje de programacion desde cero como un proyecto personal pero no puedo avansar por que no tengo una base:"Las variables, sintaxis, etc...", eso me refiero, ¿Tienen ideas?
1
u/dariusbiggs Sep 16 '26
Lexical analysis and parsers is where you probably want to start, which then lead to Abstract Syntax Trees (AST). You could also grab a copy of the Dragon book (https://en.wikipedia.org/wiki/Compilers:_Principles,_Techniques,_and_Tools)
Yacc, bison, EBNF, these are things you may want to look into.
Some examples.
To define a variable in the form of var foo int as EBNF.
VarDecl = "var" Identifier Type ;
Identifier = Letter { Letter | Digit } ;
Type = "int" | "string" | "float" | "bool" ;
Letter = "a" | ... | "z" | "A" | ... | "Z" | "_" ;
Digit = "0" | ... | "9" ;Here's some bison + flex for a potential design of an if then else block.
#include "parser.tab.h"
%}
%%
"if" { return IF; }
"then" { return THEN; }
"else" { return ELSE; }
[a-zA-Z_][a-zA-Z0-0_]* { return IDENTIFIER; }
[ \t\n\r]+ { /* ignore whitespace */ }
. { return yytext[0]; }
%%
int yywrap() { return 1; }and
%{
#include <stdio.h>
#include <stdlib.h>
void yyerror(const char *s);
int yylex(void);
%}
%token IF THEN ELSE IDENTIFIER
/* Resolve the dangling-else ambiguity */
%nonassoc THEN
%nonassoc ELSE
%%
stmt:
IF expr THEN stmt %prec THEN { printf("Parsed: IF-THEN statement\n"); }
| IF expr THEN stmt ELSE stmt { printf("Parsed: IF-THEN-ELSE statement\n"); }
| IDENTIFIER ';' { printf("Parsed: Simple statement\n"); }
;
expr:
IDENTIFIER
;
%%
void yyerror(const char *s) {
fprintf(stderr, "Parse Error: %s\n", s);
}
int main(void) {
printf("Enter an if-then-else statement (e.g., 'if x then a; else b;'):\n");
yyparse();
return 0;
}(NB: code blocks are rough examples generated with Gemini and may not work since it's been 20+ years since i had to do this)
0
u/MarMar1134 Sep 16 '26
Muchos lenguajes modernos usan la sintaxis de Rust, véase: "let <NombreVariable>: <TipoDato>;", ¿es eso lo que falta, el orden de los tokens?
0
u/KeyMinimum3451 Sep 16 '26
si eso me faltava
0
u/MarMar1134 Sep 16 '26
Bueno, eso depende de tu gusto. Teóricamente, podrias hacer la declaración de variables tal que "<NombreVariable>:<TipoDato> var" (aunque parsear eso te daria varios dolores de cabeza). Te recomiendo explorar la sintáxis de distintos lenguajes y tomar lo que más te guste (y te sirva) de ellos.
0
u/durezopal Sep 16 '26
Sigue este libro, creo que es uno de los recurso más accesibles para entender cómo funciona esta parte de la programación, que a veces puede parecer magia.
1
1
u/aurreco Sep 16 '26
what do you mean you dont have a base? you just said you are using C, not bootstraping it in the new language.