Browse Source

First import.

master
Benoît Rouits 5 months ago
parent
commit
995ac1e602
8 changed files with 3340 additions and 1 deletions
  1. +11
    -0
      Makefile
  2. +26
    -1
      README.md
  3. +2896
    -0
      eqn2tex.c
  4. +20
    -0
      eqn2tex.h
  5. +72
    -0
      eqn2tex.peg
  6. +227
    -0
      libstr.c
  7. +70
    -0
      libstr.h
  8. +18
    -0
      test.txt

+ 11
- 0
Makefile View File

@ -0,0 +1,11 @@
eqn2tex: eqn2tex.c libstr.c
cc -g eqn2tex.c libstr.c -o $@
eqn2tex.c: eqn2tex.peg
packcc $<
clean:
$(RM) eqn2tex
distclean: clean
$(RM) eqn2tex.c

+ 26
- 1
README.md View File

@ -1,3 +1,28 @@
# eqn2tex
Programme de conversion de formules mathématiques écrites en ASCII pseudo-C vers LaTeX.
Programme de conversion de formules mathématiques écrites en ASCII pseudo-C vers LaTeX.
## Pour reconstruire le code source de l'analyseur:
```
packcc eqn2tex.peg
```
## Pour construire l'exécutable:
```
make
```
## Utilisation du programme:
```
eqn2tex < test.txt
```
## Exemple:
```
dBpeak = 20*log(Qtc^2/(Qtc^2-0.25)^0.5)
se traduira en:
dBpeak = 20 \times \log{\frac{Qtc^{2}}{(Qtc^{2} - 0.25)^{0.5}}}
```

+ 2896
- 0
eqn2tex.c
File diff suppressed because it is too large
View File


+ 20
- 0
eqn2tex.h View File

@ -0,0 +1,20 @@
/* A packrat parser generated by PackCC 1.7.0 */
#ifndef PCC_INCLUDED_EQN2TEX_H
#define PCC_INCLUDED_EQN2TEX_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct calc_context_tag calc_context_t;
calc_context_t *calc_create(void *auxil);
int calc_parse(calc_context_t *ctx, char**ret);
void calc_destroy(calc_context_t *ctx);
#ifdef __cplusplus
}
#endif
#endif /* !PCC_INCLUDED_EQN2TEX_H */

+ 72
- 0
eqn2tex.peg View File

@ -0,0 +1,72 @@
%prefix "calc"
%value "char*"
%source {
#include <stdio.h>
#include <stdlib.h>
#include "libstr.h"
}
statement <- _ e:eq _ EOL { puts(e); }
/ ( !EOL . )* EOL { fprintf(stderr, "error\n"); }
eq <- l:expression _ '=' _ r:expression { $$ = str_catall(l, " = ", r, NULL); free(l); free(r); }
expression <- e:term { $$ = e; }
term <- l:term _ '+' _ r:factor { $$ = str_catall(l, " + ", r, NULL); free(l); free(r); }
/ l:term _ '-' _ r:factor { $$ = str_catall(l, " - ", r, NULL); free(l); free(r); }
/ e:factor { $$ = e; }
factor <- l:factor _ '*' _ r:power { $$ = str_catall(l, " \\times ", r, NULL); free(l); free(r); }
/ l:factor _ '/' _ r:power { $$ = str_catall("\\frac{", l, "}{", r, "}", NULL); free(l); free(r); }
/ e:power { $$ = e; }
power <- l:power _ '^' _ r:unary { $$ = str_catall(l, "^{", r, "}", NULL); free(l); free(r); }
/ e:unary { $$ = e; }
unary <- '+' _ e:unary { $$ = str_cat("+", e); free(e); }
/ '-' _ e:unary { $$ = str_cat("-", e); free(e); }
/ e:function { $$ = e; }
function <- l:fun '(' _ r:function _ ')' { $$ = str_catall("\\", l, "{", r, "}", NULL); free(l); free(r); }
/ l:fun '(' _ r:expression _ ')' { $$ = str_catall("\\", l, "{", r, "}", NULL); free(l); free(r); }
/ e:atom { $$ = e; }
greek <- 'alpha' { $$ = strdup("\\alpha"); }
/ 'Alpha' { $$ = strdup("\\Alpha"); }
/ 'beta' { $$ = strdup("\\beta"); }
/ 'Beta' { $$ = strdup("\\Beta"); }
/ 'gamma' { $$ = strdup("\\gamma"); }
/ 'Gamma' { $$ = strdup("\\Gamma"); }
/ 'delta' { $$ = strdup("\\delta"); }
/ 'Delta' { $$ = strdup("\\Delta"); }
/ 'epsilon' { $$ = strdup("\\epsilon"); }
/ 'Epsilon' { $$ = strdup("\\Epsilon"); }
/ 'theta' { $$ = strdup("\\theta"); }
/ 'Theta' { $$ = strdup("\\Theta"); }
/ 'phi' { $$ = strdup("\\phi"); }
/ 'Phi' { $$ = strdup("\\Phi"); }
/ 'pi' { $$ = strdup("\\pi"); }
/ 'Pi' { $$ = strdup("\\Pi"); }
/ 'omicron' { $$ = strdup("\\omicron"); }
/ 'Omicron' { $$ = strdup("\\Omicron"); }
/ 'sigma' { $$ = strdup("\\sigma"); }
/ 'Sigma' { $$ = strdup("\\Sigma"); }
atom <- e:greek { $$ = e; }
/ < [a-zA-Z0-9.]+ > { $$ = strdup($1); }
/ '(' _ e:expression _ ')' { $$ = str_catall("(", e, ")", NULL); free(e); }
fun <- < [a-z]+ > { $$ = strdup($1); }
_ <- [ \t]*
EOL <- '\n' / '\r\n' / '\r' / ';'
%%
int main() {
calc_context_t *ctx = calc_create(NULL);
while (calc_parse(ctx, NULL));
calc_destroy(ctx);
return 0;
}

+ 227
- 0
libstr.c View File

@ -0,0 +1,227 @@
/*
* a simplistic API to manipulate ASCII strings in C.
*
* this is part of the libstr library sources
* copyright © 2008 Benoît Rouits <brouits@free.fr>
* released under the terms of GNU LGPL
* (GNU Lesser General Public License).
* more information on http://brouits.free.fr/libstr/
* licence text on http://www.gnu.org/licenses/lgpl.txt
*/
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <string.h>
#include <sys/types.h>
#include <fnmatch.h>
#include <regex.h>
char* str_new (void)
{
/* create an empty string */
char* str;
str = malloc(1);
str[0]='\0';
return str;
}
void str_del (char* str)
{
/* delete a string */
free(str);
}
char* str_catall (const char* s, ...)
{
char *result = NULL;
if ( s != NULL )
{
va_list ap;
size_t n = 0;
va_start( ap, s );
for ( const char *p = s; p != NULL; p = va_arg( ap, const char * ) )
{
n += strlen( p );
}
va_end( ap );
++n;
result = ( char * )malloc( n );
if ( result != NULL )
{
n = 0;
va_start( ap, s );
for ( const char *p = s; p != NULL; p = va_arg( ap, const char * ) )
{
strcpy( result + n, p );
n += strlen( p );
}
va_end( ap );
}
}
return result;
}
char* str_cat (const char* str1, const char* str2)
{
/* cat two strings */
char* str;
str = malloc(strlen(str1)+strlen(str2)+1);
str[0]='\0';
strcat(str,str1);
strcat(str,str2);
return str;
}
int str_len (const char* str)
{
/* give length of string */
return strlen(str);
}
int str_end (const char* str)
{
/* give last index of string */
return strlen(str)-1;
}
char* str_up (const char* str)
{
/* capitalize string */
char* s;
int i;
s = malloc(strlen(str)+1);
for (i=0;i<strlen(str);i++)
s[i]=toupper(str[i]);
s[i]='\0';
return s;
}
char* str_cap (const char* str)
{
/* capitalize first letter */
char* s;
s = str_cat(str,"");
s[0]=toupper(str[0]);
return s;
}
char* str_low (const char* str)
{
/* lower string */
char* s;
int i;
s = malloc(strlen(str)+1);
for (i=0;i<strlen(str);i++)
s[i]=tolower(str[i]);
s[i]='\0';
return s;
}
char* str_sub (const char* str, int begin, int length)
{
/* create substring */
char* s;
s = malloc (length+1);
strncpy(s,&str[begin],length);
s[length]='\0';
return s;
}
int str_find (const char* str, const char* search)
{
/* search substing */
char *yes;
yes = strstr(str,search);
if (yes)
return yes - str;
else
return strlen(str);
}
int str_fnm (const char* str, const char* pattern)
{
/* looks fnmatch */
return !fnmatch(pattern, str, 0);
}
int str_rgm (const char* str, const char* regex)
{
/* looks re match */
regex_t *preg;
int r;
preg=malloc(sizeof(regex_t));
regcomp(preg,regex,REG_NOSUB|REG_EXTENDED);
r = regexec(preg,str,0,0,0);
regfree(preg);
free(preg);
return !r;
}
char* str_tr (const char* str, const char* from, const char* to)
{
/* translate substring (raw/raw) */
char* result;
int len1,len2,len3,len,i;
len1 = strlen(str);
len2 = strlen(from);
len3 = strlen(to);
if (len1-len2+len3 < 0)
return strdup(str);
result = malloc((len1-len2+len3+1)*sizeof(char));
len = str_find(str,from);
result[0]='\0';
if (len == len1)
return strdup(str);
strncat(result,str,len);
strncat(result,to,len3);
for (i=len+len3;i<len1-len2+len3;i++)
result[i] = str[len+len2+i-len-len3];
return result;
}
char* str_sed (const char* str, const char* regex1, const char* edit)
{
/* translate substring (regex/sedex) */
return "";
}
char** str_split (const char* str, const char* sep, char** trail)
{
/* split a string into a NULL-terminated array of substrings, given separator */
char** array;
char* string;
const char* s;
char* e;
int i;
array = NULL;
i = 0;
s = str;
while ((e = strstr(s, sep))) {
++i;
string = strndup(s, e-s);
s = e + strlen(sep);
array = realloc(array, i*sizeof(char*));
array[i-1] = string;
}
++i;
array = realloc(array, i*sizeof(char*));
array[i-1] = NULL;
*trail = strndup(s, (s-str)+strlen(str));
return array;
}

+ 70
- 0
libstr.h View File

@ -0,0 +1,70 @@
/*
* public interface of libstr.
*
* a simplistic API to manipulate ASCII strings in C.
*
* this is part of the libstr library sources
* copyright © 2008 Benoît Rouits <brouits@free.fr>
* released under the terms of GNU LGPL
* (GNU Lesser General Public License).
* more information on http://brouits.free.fr/libstr/
* licence text on http://www.gnu.org/licenses/lgpl.txt
*/
#ifndef LIBSTR_H
#define LIBSTR_H
#ifdef __cplusplus
extern "C" {
#endif
extern char* str_new (void);
/* create an empty string */
extern void str_del (char* str);
/* delete a string */
extern char* str_cat (const char* str1, const char* str2);
/* cat two strings */
char* str_catall (const char* first, ...);
/* cat n strings */
extern int str_len (const char* str);
/* give length of string */
extern int str_end (const char* str);
/* give last index of string */
extern char* str_up (const char* str);
/* capitalize string */
extern char* str_cap (const char* str);
/* capitalize first letter */
extern char* str_low (const char* str);
/* lower string */
extern char* str_sub (const char* str, int begin, int length);
/* create substring */
extern int str_find (const char* str, const char* search);
/* search substing */
extern int str_fnm (const char* str, const char* pattern);
/* looks fnmatch */
extern int str_rgm (const char* str, const char* regex);
/* looks re match */
extern char* str_tr (const char* str, const char* from, const char* to);
/* translate substring (raw/raw) */
extern char* str_sed (const char* str, const char* regex1, const char* edit);
/* tranlate substring (regex/regex) */
char** str_split (const char* str, const char* sep, char** trail);
/* split a string given a separator */
#ifdef __cplusplus
}
#endif
#endif

+ 18
- 0
test.txt View File

@ -0,0 +1,18 @@
enclosure=sealed
F3 = Fb*((1/Qtc^2-2+((1/Qtc^2-2)^2+4)^0.5)/2)^0.5
dBpeak = 20*log(Qtc^2/(Qtc^2-0.25)^0.5)
dBmag = 10*log(Fr^2/((Fr-1)^2+Fr/Qtc^2))
enclosure=ported
F3 = (Vas/Vb)^0.44*Fs
dBpeak = 20*log(Qts*(Vas/Vb)^0.3/0.4)
Fn2 = (F/Fs)^2
Fn4 = Fn2^2
A = (Fb/Fs)^2
B = A/Qts+Fb/(Fs*Ql)
C = 1+A+(Vas/Vb)+Fb/(Fs*Qts*Ql)
D = 1/Qts+Fb/(Fs*Ql)
dBmag = 10*log(Fn4^2/((Fn4-C*Fn2+A)^2+Fn2*(D*Fn2-B)^2))
Lv = (23562.5*Dv^2*Np/(Fb^2*Vb))-(k*Dv)
Sd = pi*(Dia/100)^2/4
Vd = Sd*Xmax/1000
Dmin = 100*(20.3*(Vd^2/Fb)^0.25)/Np^.5

Loading…
Cancel
Save