Tuesday, July 2, 2019

Digest for comp.lang.c++@googlegroups.com - 21 updates in 3 topics

Bonita Montero <Bonita.Montero@gmail.com>: Jul 02 08:50AM +0200


>>> No.
 
>> No, sure.
 
> There is a difference between static_cast and reinterpret_cast. No?
 
That wasn't the question. The question is if everything which can
be done with C++-style casts can be done with C-style casts - and
that's almost true.
Juha Nieminen <nospam@thanks.invalid>: Jul 02 08:14AM

> C++-style casts are superfluous child-proof locks in most cases.
> And in many cases they're even syntactic sugar.
 
You could say that of the entirety of C++. Just program directly
in asm.
 
Heck, even asm is just syntactic sugar. Just program directly in
machine code by writing the byte values into an executable file
using a hex editor.
Bonita Montero <Bonita.Montero@gmail.com>: Jul 02 11:30AM +0200

>> And in many cases they're even syntactic sugar.
 
> You could say that of the entirety of C++. Just program directly
> in asm.
 
That's an not fitting analogy. C++-style casts just don't have an
advantage on the maintainability, deveopment-performance and ease
of writing.
"Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com>: Jul 02 12:20PM +0200

On 02.07.2019 11:30, Bonita Montero wrote:
 
> That's an not fitting analogy. C++-style casts just don't have an
> advantage on the maintainability, deveopment-performance and ease
> of writing.
 
I hate to agree with Juha, since he has adopted the sideline hobby of
bashing me at any opportunity, but he's right.
 
The `++` in C++ is nearly all about imposing constraints on the code,
mostly via static typing, and the named casts help with that.
 
For example, `static_cast` does a static cast and no other kind of cast.
 
 
Cheers!,
 
- Alf
Bart <bc@freeuk.com>: Jul 02 11:51AM +0100

On 02/07/2019 09:14, Juha Nieminen wrote:
 
> Heck, even asm is just syntactic sugar. Just program directly in
> machine code by writing the byte values into an executable file
> using a hex editor.
 
A hex editor? What luxury!
 
I used LED lights to show binary contents of memory locations, and an
earth lead to touch 8 metal contacts to set individual bits (couldn't
afford so many push-buttons) to program in binary machine code.
 
(This was actually quite true.)
Bonita Montero <Bonita.Montero@gmail.com>: Jul 02 01:25PM +0200

> The `++` in C++ is nearly all about imposing constraints on the
> code, mostly via static typing, and the named casts help with that.
 
No, they rarely make things more readable or maintainable.
 
> For example, `static_cast` does a static cast and no other kind of cast.
 
C++-style casts are mostly an idea for compulsive developers;
and compulsiveness is an occupational illness of may developers.
Ben Bacarisse <ben.usenet@bsb.me.uk>: Jul 02 02:35PM +0100

>> using a hex editor.
 
> A hex editor? What luxury!
 
> I used LED lights to show binary contents of memory locations,
 
<accent location=northern>
LED lights? Luxury! Bloody luxury! When I were a lad we had to lick
terminals on 't delay lines wi' our tongues to read memory.
</accent>
 
--
Ben.
Christian Gollwitzer <auriocus@gmx.de>: Jul 02 04:47PM +0200

Am 02.07.19 um 15:35 schrieb Ben Bacarisse:
> LED lights? Luxury! Bloody luxury! When I were a lad we had to lick
> terminals on 't delay ines wi' our tongues to read memory.
> </accent>
 
 
https://xkcd.com/378/
James Kuyper <jameskuyper@alumni.caltech.edu>: Jul 02 07:59AM -0700

On Tuesday, July 2, 2019 at 9:35:21 AM UTC-4, Ben Bacarisse wrote:
> Bart <bc@freeuk.com> writes:
...
> LED lights? Luxury! Bloody luxury! When I were a lad we had to lick
> terminals on 't delay lines wi' our tongues to read memory.
> </accent>
 
How far north? Greenlandic? :-)
"Shadwell hated all Southerners, and by inference, was standing at the
north pole." ("Good Omens" - Terry Pratchet).
G G <gdotone@gmail.com>: Jul 02 09:10AM -0700

James:
 
Hahahahaha, that was funny. "...Standing at the North Pole...". ...hated all southerners ...
 
Cause I had to read that twice .
 
There is humor in this group. :- )
James Kuyper <jameskuyper@alumni.caltech.edu>: Jul 02 10:06AM -0700

On Tuesday, July 2, 2019 at 12:10:23 PM UTC-4, G G wrote:
 
> Hahahahaha, that was funny. "...Standing at the North Pole...". ...hated all southerners ...
 
> Cause I had to read that twice .
 
> There is humor in this group. :- )
 
It can be hard to be sure on usenet whether or not a comment is intended
to be sarcastic unless it's specifically labelled as such - your wording
gives me some doubt about it. If that wasn't sarcastic, I strongly
recommend reading anything by Terry Pratchet - there's lots more where
that came from, and that's far from being the funniest line he's written.
Melzzzzz <Melzzzzz@zzzzz.com>: Jul 02 05:38PM


> That's an not fitting analogy. C++-style casts just don't have an
> advantage on the maintainability, deveopment-performance and ease
> of writing.
Not true.
 
--
press any key to continue or any other to quit...
U ničemu ja ne uživam kao u svom statusu INVALIDA -- Zli Zec
Na divljem zapadu i nije bilo tako puno nasilja, upravo zato jer su svi
bili naoruzani. -- Mladen Gogala
Bonita Montero <Bonita.Montero@gmail.com>: Jul 02 07:48PM +0200

>> advantage on the maintainability, deveopment-performance and ease
>> of writing.
 
> Not true.
 
No, true. ;-)
Vir Campestris <vir.campestris@invalid.invalid>: Jul 02 09:16PM +0100

On 02/07/2019 18:06, James Kuyper wrote:
> gives me some doubt about it. If that wasn't sarcastic, I strongly
> recommend reading anything by Terry Pratchet - there's lots more where
> that came from, and that's far from being the funniest line he's written.
 
Pratchett is always worth a read. You'll even notice he understands
computer people - if you read about the clacks.
 
Andy
--
Who doesn't go past core store and dip switches - and that was only once...
G G <gdotone@gmail.com>: Jul 02 03:50PM -0700

> gives me some doubt about it. If that wasn't sarcastic, I strongly
> recommend reading anything by Terry Pratchet - there's lots more where
> that came from, and that's far from being the funniest line he's written.
 
James:
 
no i thought that was funny... really!
 
i will look up Terry Pratchet.
 
i've been watching amazon prime stuff lately, and have found
Canadian tv very funny, the last few days. so i was in the comedic
mindset when i read it i guess. comedies like : amazon: corner gas,
youtube: little mosque on the prairie ,
bbc: yes minister and yes prime minister.
 
who knew canada could tell a good joke (just kidding about
canada telling a good joke)

those shows have me laughing out loud.
 
yes i enjoyed the joke. no sarcasm here.
"Chris M. Thomasson" <invalid_chris_thomasson_invalid@invalid.com>: Jul 02 03:51PM -0700

On 7/2/2019 1:14 AM, Juha Nieminen wrote:
 
> Heck, even asm is just syntactic sugar. Just program directly in
> machine code by writing the byte values into an executable file
> using a hex editor.
 
The Woz! :^)
"Chris M. Thomasson" <invalid_chris_thomasson_invalid@invalid.com>: Jul 02 03:52PM -0700

On 7/2/2019 6:35 AM, Ben Bacarisse wrote:
> LED lights? Luxury! Bloody luxury! When I were a lad we had to lick
> terminals on 't delay lines wi' our tongues to read memory.
> </accent>
 
LOL! :^D
alexo <alelvb@inwind.it>: Jul 02 07:59PM +0200

Il 02/07/19 19:06, Stefan Ram ha scritto:
>> CuSO4*5H2O
 
> I do not know how crystallization should be parsed,
> therefore, I ignored crystallization.
 
take for example CuSO4*5H2O
 
the number 5 following the '*' character is the number of molecules
that follow it, in that case H2O, so the parser should read
 
10 Hydrogen atoms
5 Oxygen atoms
 
 
> How to do this evaluation depends on the kind of evaluation
> wanted here. I don't know what kind of evaluation is wanted
> here.
 
I'm Sorry but I don't know what a syntax tree is. I know what an
informatic tree is but I never implemented one myself.
 
I've published the code of my parser in the hope that someone could
suggest me improves, but I think that the group thinks I'm a lazy
student :( but that's not so. I'm not a student anymore. I've had my
laurea in chemistry 19 years ago.
 
I have to say moreover that I didn't understand the code you wrote some
days ago. It's full of things I don't yet know. But as I've already told
it works.
 
Anyway a big thank you for your interest.
 
alessandro
alexo <alelvb@inwind.it>: Jul 02 09:52PM +0200

Il 02/07/19 20:22, Stefan Ram ha scritto:
 
> [.. code omitted ..]
 
 
that is amazing! Progrmming is amazing!
 
--- Warning --- personal history follows.
if you are not interested in my life, skip reading further.
You've been advised ;)
 
When I was a kid my grandpa bought me a C64. I live in Italy and at that
times I was about 7 or 8 years old. I remember that besides the games
that I never ended, I tried to make some BASIC code programs working,
but with no or few success. I was always making transcription errors.
Moreover the instructions were in English - I couldn't speak nor read it
yet, and those listings were not 'Hello world' programs, but several
pages long and I never had the satisfaction of making something of them
working. So my experience with computers was initially frustrating. When
I chose the university faculty, I opted for chemistry, but it was only
at the 3rd year (in 1997) that I had the possibility to study a
programming language. It was Fortran77 - the teacher was not up-to-date
in the IT world - and that language even if old, opened me a world. But
it was too late. My study road was at half a half and it was late to
change. But what remained from that days was the passion. My first
'hobbyist' programming attempts are dated back to the very first
years of 2000's whit Java.
 
But my step were:
 
learning a bit of Java -> learning a bit C -> learning C++ (still in
progress)
 
Mr Stroustrup writes that it's not necessary to learn C before learning
C++ but to me was nonsense learning OO programming (that anyway uses
structured statements) without knowing anything about structured
programming. And Java was born to avoid pointers that are essential in C
and C++. So since this languages share the same syntax I wanted to know
more about C.
Now java is set aside, but I feel still at a beginner level both with C
and C++.
 
I know a bit about linked lists and I've read two sorting algorithms
merge and quick sort.
actually I've written two GUI interfaces in FLTK and a few more things
 
Even if I read of programming from about 20 years I have to admit that
I'm not constant in writing code nor in the point were I'm.
 
My parser was a brute-force approach. It reads the formula left to right
like a human does. and recursively calls itself to analyze blocks inside
parentheses. But something is not working as I expected. Maybe I'll be
able to fix it or maybe not. It doesn't matter. Its a hobby.
 
thank to you all in the group.
ram@zedat.fu-berlin.de (Stefan Ram): Jul 02 05:06PM

>But to be perfect it should handle even the molecules of crystallization
>that are written in this way (but I didn't talk about them in my post:
>CuSO4*5H2O
 
I do not know how crystallization should be parsed,
therefore, I ignored crystallization.
 
In the meantime, I have developed another idea:
 
We parse to build a syntax tree in memory and then
evaluate the syntax tree somehow.
 
But a formula like "[Fe(CO)6][Sb2F11]2" already /is/ a
syntax tree from a certain point of view. So, I wonder
whether a parser is needed at all! Instead, one can just
decorate the tree with some functions for traversal and
then start to do the evaluation directly on this tree
which is given as (for example) "[Fe(CO)6][Sb2F11]2".
 
How to do this evaluation depends on the kind of evaluation
wanted here. I don't know what kind of evaluation is wanted
here.
ram@zedat.fu-berlin.de (Stefan Ram): Jul 02 06:22PM

>I'm Sorry but I don't know what a syntax tree is. I know what an
>informatic tree is but I never implemented one myself.
 
A syntax tree (usually called "abstract syntax tree", AST) is
an internal representation of the structure of parsed text.
 
>I have to say moreover that I didn't understand the code you wrote some
>days ago. It's full of things I don't yet know. But as I've already told
>it works.
 
I wrote a small tutorial about parsing in C, here it is:
 
(If answering to this following post, one should please not
quote all of it, but only a few lines one directly refers to.)
 
In order to interpret or translate an expression (term), it is
decomposed into lexical units (tokens, words), which then are
used by a parser to build symbols and a structured
representation of the input. This representation then might be
evaluated or translated into some other representation.
 
The syntactial structuring resembles the rules for the
construction of an expression, which often is given by so-
called "productions" of the EBNF (extended Backus-Nauer-Form)
and which sometimes are left-recursive.
 
When writing a parser, the left-recursive productions sometimes
are a worry to the author, because it is not obvious how to
avoid an infinite recursion. The solution is to rewrite them as
right-recursive productions.

The addition with a binary infix Operator, for example, is
left associative. However, it is simpler to analyze in a
right-associative manner. Therefore, one analyzes the source
using right-associative rules and then creates a result
using a left-associative interpretation.

A left-associative grammar might be, for example, as follows.
 
<numeral> ::= '2' | '4' | '5'.
<expression> ::= <numeral> | <expression> '+' <numeral>.
start symbol: <expression>.
 
To analyze this using a recursive descent parser, one
prefers to use the following grammar.
 
<numeral> ::= '2' | '4' | '5'.
<expression> ::= <numeral>[ '+' <expression> ].
start symbol: <expression>.
 
This can be written using iteration as follows.
 
<numeral> ::= '2' | '4' | '5'.
<expression> ::= <numeral>{ '+' <numeral> }.
start symbol: <expression>.
 
However, the product is created in the sense of the
first grammar. Example code follows.
 
#include <stdio.h> /* printf */
 
/* scanner */
 
static inline char get()
{ static char const * const source = "2+4+5)";
static int pos = 0;
return source[ pos++ ]; }
 
/* parser */
 
static inline int numeral(){ return get() - '0'; }
 
static int sum(){ int result = numeral();
while( '+' == get() )result += numeral();
return result; }
 
/* main */
 
int main( void ){ printf( "sum = %d\n", sum() ); }
 
To be able to parse expressions with higher
priority, the grammar can be extended.
 
<numeral> ::= '2' | '4' | '5'.
<product> ::= <numeral> | <product> '*' <numeral>.
<sum> ::= <product> | <sum> '+' <product>.
start symbol: <sum>.
 
In iterative notation:

<numeral> ::= '2' | '4' | '5'.
<product> ::= <numeral>{ '*' <numeral> }.
<sum> ::= <product>{ '+' <product> }.
start symbol: <sum>.
 
In C:
 
#include <stdio.h> /* printf */
 
/* scanner */
 
static inline char get( int const move )
{ static char const * const source = "2+4*5)";
static int pos = 0;
char result = source[ pos ]; pos += move; return result; }
 
/* parser */
 
static inline int numeral(){ return get( 1 )- '0'; }
 
static int product(){ int result = numeral();
while( '*' == get( 0 )){ get( 1 ); result *= numeral(); }
return result; }
 
static int sum(){ int result = product();
while( '+' == get( 1 ))result += product();
return result; }
 
/* main */
 
int main( void ){ printf( "sum = %d\n", sum() ); }
 
Exercises
 
- What is the output of the above programs?
 
- Extend the last grammar and the last program so as
to handle subtraction.
 
- Extend the result of the last exercise in order
to handle division.
 
- Extend the result of the last exercise so that also
numbers with multiple digits are accepted.
 
- Extend the result of the last exercise so that also
terms in parentheses are accepted. The input "(2+4)*5)"
should give the result "30".
 
- Extend the result of the last exercise so that
also a unary minus "-" is recognized.

- Extend the result of the last exercise so that
more operators and functions are recognized.
 
- Extend the result of the last exercise so that
meaningful error messages are created for all
inputs that do not fulfill the rules of the input
language.
 
- Extend the result of the last exercise so that the
error messages also show the location where the error
was detected. It should be possible to enter an expression
that spans multiple lines, and an error message should
contain the number of the line where the error was
detected.

See also:
 
http://compilers.iecc.com/crenshaw/
 
So far for the small C parser tutorial.
 
I also wrote a C++ parser in the spirit of the above
C parser that uses special features of C++. But this
code is more compact and therefore possibly more
difficult to understand.
 
#include <iostream>
#include <ostream>
#include <map>
#include <functional>
#include <cmath>
#include <sstream>
#include <cstring>
 
::std::map< char, ::std::function< double( double, double )>> ex
{ { '^', []( double const x, double const y )->double
{ return ::std::pow( x, y ); }},
{ '*', []( double const x, double const y )->double{ return x * y; }},
{ '/', []( double const x, double const y )->double{ return x / y; }},
{ '+', []( double const x, double const y )->double{ return x + y; }},
{ '-', []( double const x, double const y )->double{ return x - y; }}};
 
::std::map< char, bool >left_associative
{ { '^', false }, { '*', true }, { '/', true }, { '+', true }, { '-', true }};
 
template< typename scanner >struct parser
{ scanner s; parser( char const * s ): s{ s } {}
char check( char const * const op )
{ return ::std::strchr( op, static_cast< char >( s.peek() )) ?
static_cast< char >( s.get() ): 0; }
double numeral(){ return s.get() - '0'; }
double prefix(){ int sign = 1;
while( '-' == s.peek() ){ s.get(); sign *= -1; } return sign * numeral(); }
double parse( char const * const op, double( ::parser< scanner >::*next ) () )
{ double result =( this->*next )();
for( char sym; sym = check( op ); )result = ex[ sym ]
( result, left_associative[ sym ]?( this->*next )(): parse( op, next ));
return result; };
double power(){ return parse( "^", &::parser< scanner >::prefix ); }
double product(){ return parse( "*/", &::parser< scanner >::power ); }
double sum(){ return parse( "+-", &::parser< scanner >::product ); }
double start(){ return sum(); }};
 
int main()
{ using p = ::parser< ::std::stringstream >;
auto test = []( char const * s ){ ::std::cout << p{ s }.start() << '\n'; };
test( "2^2^3/2/2" ); test( "2+2*2^2^-3" ); }
 
But both example parsers do not actually build a syntax tree
in memory but rather evaluate the given expression.
 
A (binary) tree is a value that might contain pointers to two
other trees: the left subtree and the right subtree.
 
One can imagine it like
 
3
/ \
/ \
1 4
/ \
/ \
0 2
 
(in compact notation: »(((0)1(2))3(4))«).
 
It can be build in C as
 
typedef struct tree
{ struct tree * left; int value; struct tree * right; } * TREE;
extern struct tree t1, t0, t2, t4; struct tree t3 =
{ &t1, 3, &t4 },
t1 ={ &t0, 1, &t2 }, t4 ={ 0, 4, 0 },
t0 ={ 0, 0, 0 }, t2 ={ 0, 2, 0 };
 
Now, the three is »t3«.
 
A program that actually builds a tree in memory from a
tree expression given at runtime can look like
 
#include <stdio.h> /* printf, putchar */
#include <stdlib.h> /* EXIT_SUCCESS, abort */
 
/* tree */
typedef struct tree
{ struct tree * left;
int value;
struct tree * right; } * TREE;
 
TREE newtree( TREE const left, int const value, TREE const right )
{ TREE const result = malloc( sizeof *result );
if( !result )abort();
result->left = left;
result->value = value;
result->right = right;
return result; }
 
/* scanner */
char get( int const move )
{ static char const * const source = "(((0)1(2))3(4))";
static int pos = 0;
char const result = source[ pos ];
if( result )pos += !!move;
return result; }
 
/* parser */
int number( void ){ return get( 1 )- '0'; }
TREE tree( void );
TREE entry( void )
{ TREE left, right; int value;
get( '(' );
left = tree();
value = number();
right = tree();
get( ')' );
return newtree( left, value, right ); }
 
TREE tree( void )
{ return get( 0 )== '(' ? entry() : 0; }
 
/* serializer */
void print( const TREE const tree )
{ if( tree )
{ putchar( '(' ); print( tree->left ); printf( "%d", tree->value );
print( tree->right ); putchar( ')' ); }}
 
/* main */
int main( void )
{ print( tree() ); putchar( '\n' );
return EXIT_SUCCESS; }
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: