How to create Antlr tree Parser Diagram through CMD Line - parsing

How to create Antlr tree Parser Diagram through CMD Line?
Currently i have some grammar content which am trying to parse as Tree in Antlr.
Currently am executing this grammar file thriugh CMD line
C:\Users\mohan\ANTLR>java -jar antlr4-4.1-complete.jar grammar.g4

After creating the Lexer and Parser java files i recommand to use the ANTLR testing tool taht provided in the ANTLR jar org.antlr.v4.gui.TestRig
java -cp .;D:\Work\lib\antlr-4.5.3-complete.jar org.antlr.v4.gui.TestRig <GrammarName> <startRuleName> -gui [input-filename]
should do the trick.
you can read more about this in The Definitive ANTLR 4 Reference its have more details on how to use it.
pesonaly i prefer using notepad++ ANTLR plugin to test my grammar its basicly does the same thing except its more comfterble for me not to always go back to the Command Prompt after every change in my grammar.
here is a link to the notepad++ plugin and how to install it :notepad++ antlr pigin

After writing my grammar file for my corresponding code block, i have compiled and executed as follows
set path=%path%;C:\Program Files\Java\jdk1.8.0_92\bin
set path=%path%;C:\Users\Mohan\ANTLR\antlr-4.5.3-complete.jar
java -jar antlr-4.5.3-complete.jar Grammar.G4
javac -cp .;antlr-4.5.3-complete.jar Grammar*.java
java -cp .;antlr-4.5.3-complete.jar org.antlr.v4.gui.TestRig Grammar prog -gui
C:\Users\Mohan\ANTLR> java -cp .;antlr-4.5.3-complete.jar org.antlr.v4.gui.TestRig Grammar prog -gui
After Compiling all commands in Command prompt give Ctrl + Z if you are using Windows and Ctrl + D if you are using **nix, to generate parser diagram.

Related

Parsing Haskell file with CPP macros fails

I am using GHC 9.0.2 and I am trying to compile a file like this with haskell-src-exts-1.23.1:
Main.hs
~~~~~~~
{-# LANGUAGE CPP #-}
module Main where
#define MSG "Hello world!"
main :: IO ()
main = putStrLn MSG
The basic compilation with compiler works just fine: ghc Main.hs. But try to do the same with parseFile:
ghci> import Language.Haskell.Exts
ghci> parseFile "Main.hs"
ParseFailed (SrcLoc "Main.hs" 5 1) "Parse error: #"
Even using parseFileWithExts [EnableExtension CPP] "Main.hs" gives the same result.
So the question is: how to parse Haskell files with CPP macros inside of them?
haskell-src-exts in itself is not concerned with calling the preprocessor, so the parser fails when it sees syntax of the preprocessor (#define...). There's a separate package that wraps a call to the preprocessor — hse-cpp. With the Main.hs as in the question, this works:
$ cabal install --lib haskell-src-exts hse-cpp --package-env=.
...
$ ghci
...
> import Language.Haskell.Exts
> import Language.Haskell.Exts.CPP
> parseFileWithCommentsAndCPP defaultCpphsOptions defaultParseMode "Main.hs"
ParseOk (Module ...

download file whith powershell v 2.0

I hope I am not out of OT I have a problem with powershell v2.0 with win 7 I am writing a dos batch file whose task, among other things, is to download a file. To do this I am using powershell 2.0 by win 7. I don't know this language and I can't go on anymore.
powershell.exe -NoExit -Command "& {(New-Object System.Net.WebClient).DownloadFile("https://downloads.html/example.exe","C:\tmp\example.exe")}"
the error he gives me is the following
')' missing in the method call. In row: 1 car: 52
& {((New-Object System.Net.WebClient) .DownloadFile (<<<< https://downloads.ar
duino.cc/arduino-1.8.13-windows.exe,C:\tmp\arduino-1.8.12-windows.exe))}
CategoryInfo: ParserError: (CloseParenToken: TokenId) [], Paren
tContainsErrorRecordException
FullyQualifiedErrorId: MissingEndParenthesisInMethodCall
thanks
The syntax is
(New-Object System.Net.WebClient).DownloadFile($url, $output)
I didn't run your code but it looks like you're missing a parentheses before ".download".

How can I compile an Erlang file with includes from the erl shell?

erlc -I <abspath-to-include-dir> <module>.erl on the command line correctly compiles <module>
But in the Erlang shell (erl), the following produces errors with "cannot find include file":
c(<module>, [{i, <abspath-to-include-dir>}]).
Why? Shouldn't the behavior of these two ways of compiling files be the same?
Try writing the path as a list of directories, as in {i, [".../here/", ".../there/"]}, even if it's just a single directory.

How to generate parse tree from antlr with command

My file name is:
Hello.g4
Grammar is:
grammar Hello;
r : 'hello' ID ;
ID : [a-z]+ ;
WS : [ \t\r\n]+ -> skip ;
Antlr is used for the parse tree, but I don't know what the command to actually generate the parse tree is.
Here's what I do. First, compile the grammar using a batch file. Your classpath variable may be different based on where your jar file is located. This is for Windows:
SET CLASSPATH=.;D:\jarfiles\antlr-4.7-complete.jar;
java org.antlr.v4.Tool %*
Next you have to compile the resulting files:
javac Hello*.java
Finally, run the TestRig tool. Again, I use a batch file.
java org.antlr.v4.gui.TestRig %*
When you run TestRig, you supply the grammar name, the starting rule r in this case, and either type in the input or supply an input file. the -tokens command-line option will show how your input is lexed; the -gui option will produce a graphical parse tree.

How do I run a beam file compiled by Elixir or Erlang?

I have installed Erlang/OTP and Elixir, and compiled the HelloWorld program into a BEAM using the command:
elixirc test.ex
Which produced a file named Elixir.Hello.beam
How do I run this file?
Short answer: no way to know for sure without also knowing the contents of your source file :)
There are a few ways to run Elixir code. This answer will be an overview of various workflows that can be used with Elixir.
When you are just getting started and want to try things out, launching iex and evaluating expressions one at a time is the way to go.
iex(5)> Enum.reverse [1,2,3,4]
[4, 3, 2, 1]
You can also get help on Elixir modules and functions in iex. Most of the functions have examples in their docs.
iex(6)> h Enum.reverse
def reverse(collection)
Reverses the collection.
[...]
When you want to put some code into a file to reuse it later, the recommended (and de facto standard) way is to create a mix project and start adding modules to it. But perhaps, you would like to know what's going on under the covers before relying on mix to perform common tasks like compiling code, starting applications, and so on. Let me explain that.
The simplest way to put some expressions into a file and run it would be to use the elixir command.
x = :math.sqrt(1234)
IO.puts "Your square root is #{x}"
Put the above fragment of code into a file named simple.exs and run it with elixir simple.exs. The .exs extension is just a convention to indicate that the file is meant to be evaluated (and that is what we did).
This works up until the point you want to start building a project. Then you will need to organize your code into modules. Each module is a collection of functions. It is also the minimal compilation unit: each module is compiled into a .beam file. Usually people have one module per source file, but it is also fine to define more than one. Regardless of the number of modules in a single source file, each module will end up in its own .beam file when compiled.
defmodule M do
def hi(name) do
IO.puts "Hello, #{name}"
end
end
We have defined a module with a single function. Save it to a file named mymod.ex. We can use it in multiple ways:
launch iex and evaluate the code in the spawned shell session:
$ iex mymod.ex
iex> M.hi "Alex"
Hello, Alex
:ok
evaluate it before running some other code. For example, to evaluate a single expression on the command line, use elixir -e <expr>. You can "require" (basically, evaluate and load) one or more files before it:
$ elixir -r mymod.ex -e 'M.hi "Alex"'
Hello, Alex
compile it and let the code loading facility of the VM find it
$ elixirc mymod.ex
$ iex
iex> M.hi "Alex"
Hello, Alex
:ok
In that last example we compiled the module which produced a file named Elixir.M.beam in the current directory. When you then run iex in the same directory, the module will be loaded the first time a function from it is called. You could also use other ways to evaluate code, like elixir -e 'M.hi "..."'. As long as the .beam file can be found by the code loader, the module will be loaded and the appropriate function in it will be executed.
However, this was all about trying to play with some code examples. When you are ready to build a project in Elixir, you will need to use mix. The workflow with mix is more or less as follows:
$ mix new myproj
* creating README.md
* creating .gitignore
* creating mix.exs
[...]
$ cd myproj
# 'mix new' has generated a dummy test for you
# see test/myproj_test.exs
$ mix test
Add new modules in the lib/ directory. It is customary to prefix all module names with your project name. So if you take the M module we defined above and put it into the file lib/m.ex, it'll look like this:
defmodule Myproj.M do
def hi(name) do
IO.puts "Hello, #{name}"
end
end
Now you can start a shell with the Mix project loaded in it.
$ iex -S mix
Running the above will compile all your source file and will put them under the _build directory. Mix will also set up the code path for you so that the code loader can locate .beam files in that directory.
Evaluating expressions in the context of a mix project looks like this:
$ mix run -e 'Myproj.M.hi "..."'
Again, no need to compile anything. Most mix tasks will recompile any changed files, so you can safely assume that any modules you have defined are available when you call functions from them.
Run mix help to see all available tasks and mix help <task> to get a detailed description of a particular task.
To specifically address the question:
$ elixirc test.ex
will produce a file named Elixir.Hello.beam, if the file defines a Hello module.
If you run elixir or iex from the directory containing this file, the module will be available. So:
$ elixir -e Hello.some_function
or
$ iex
iex(1)> Hello.some_function
Assume that I write an Elixir program like this:
defmodule PascalTriangle do
defp next_row(m), do: for(x <- (-1..Map.size(m)-1), do: { (x+1), Map.get(m, x, 0) + Map.get(m, x+1, 0) } ) |> Map.new
def draw(1), do: (IO.puts(1); %{ 0 => 1})
def draw(n) do
(new_map = draw(n - 1) |> next_row ) |> Map.values |> Enum.join(" ") |> IO.puts
new_map
end
end
The module PascalTriangle can be used like this: PascalTriangle.draw(8)
When you use elixirc to compile the ex file, it will create a file called Elixir.PascalTriangle.beam.
From command line, you can execute the beam file like this:
elixir -e "PascalTriangle.draw(8)"
You can see the output similar to the photo:

Resources