Pascal code checker!

S

SMALLp

Hy! I desperately need help!

I need to make application that would accept Pascal code and check if it
returns good results.

My idea is (from a beginner point of view) to make application in python
that would send code (text) to pascal compiler (Free pascal compiler
that can be used from command prompt in windows) and it would return
result and then application would show that result.

Problem starts Here. I have no idea what to search for and where to even
start. So what i need is some help (better say ideas) what to search and
what to do. Please help, I have a lot of time and will to learn. I only
need to know what.

Thanks in advance!
 
T

Tim Chase

I need to make application that would accept Pascal code and check if it
returns good results.

well, it depends on how much work you want your program to do.
My understanding is that most compilers will return a non-zero
error code when there's some sort of problem. Thus, you could
just shell out to compile the code and check the resulting code.
Time to implementation: likely less than 20 minutes, testing
included.

OTOH, you could actually parse the pascal file(s). This gives
you infinte flexibility to do whatever you want with the
parse-tree. Things get hairy when you have included ("uses")
files, and you also have to write up a grammar for Pascal that
can be understood by Python. The common wisdom seems to be to
use PyParsing[1] for such parsing tasks. You'd also have to
decide which flavor of Pascal you intend to parse (pascal, turbo
pascal, object pascal, delphi, etc). You might be able to use
some of the parser definitions from the Free Pascal[2] project as
a model on which to base your parser's syntax. Time to break out
the old Pascal language rail-road diagrams from CSC101.
Time to implementation: weeks or months or even years

As you can see, if you can get an existing pascal parser to do
the work for you, and all you need to know is whether it built
successfully, option "A" should be your obvious choice. If you
have some deeper need to wander around this parsed structure,
you're stuck with the labor involved in option "B".

-tkc

[1] pyparsing.wikispaces.com
[2] www.freepascal.org
 
G

Grant Edwards

Hy! I desperately need help!

I need to make application that would accept Pascal code and check if it
returns good results.

That application is called a Pascal compiler.
My idea is (from a beginner point of view) to make application in python
that would send code (text) to pascal compiler (Free pascal compiler
that can be used from command prompt in windows) and it would return
result and then application would show that result.

I don't see why you need to write a Python program at all.
Problem starts Here. I have no idea what to search for and where to even
start.

I'd Google for "pascal compiler"
 
P

Paul McGuire

OTOH, you could actually parse the pascal file(s). This gives
you infinte flexibility to do whatever you want with the
parse-tree. Things get hairy when you have included ("uses")
files, and you also have to write up a grammar for Pascal that
can be understood by Python. The common wisdom seems to be to
use PyParsing[1] for such parsing tasks. ...

Oof! Fond as I am of promoting pyparsing, writing a Pascal compiler
(or even just syntax checker) is not a job I would tackle lightly,
much less suggest to a new Python developer.

A Pascal *subset* might be a good first step, and check for
feasibility. But the shell-out-to-a-compiler strategy has a much
higher chance of success.

-- Paul
 
T

Tim Chase

Oof! Fond as I am of promoting pyparsing, writing a Pascal
True enough...thus my weeks/months/years estimate for such an
undertaking :)

But if not mentioned, you know the next question would be "how do
I create a regular expression to match valid Pascal programs?"

:-o

-tkc
 
S

Steven D'Aprano

True enough...thus my weeks/months/years estimate for such an
undertaking :)

But if not mentioned, you know the next question would be "how do I
create a regular expression to match valid Pascal programs?"

A *one-line* regular expression.
 
N

Neil Cerutti

Go track down the source code of an open source text editor
that supports Pascal highlighting. Kate comes to mind. It's not
written in Python, but it will have Pascal parsing code for the
highlighter.

A syntax highlighter usually knows very little about the syntax
of a language. Mostly, it just finds keywords and highlights
them. The improvements beyond that will generally be haphazard,
e.g., the Vim syntax highlighting code for Python knows basically
what a function header looks like so it can highlight the
function name. It also (doesn't really) know how to figure out
what's a string and what isn't.

The auto-indenter is often smarter about syntax, but knows just a
small subset of syntax rules, enought to do proper indenting. For
some languages, e.g., Python, that's a fairly small subset. For
others, it's cumbersomely large and Vim's support is cruddy.
 
Z

Zentrader

The OP was not talking about a Python program to check the Pascal
program as I read it
to make application in python
that would send code (text) to pascal compiler...and it would return
result and then application would show that result.

So he/she/it would want subprocess to compile the Pascal program/file
and get the resulting output. Let's not complicate the issue since
this appears to be someone relatively new to Python.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top